dca.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2007 - 2009 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in
  15. * the file called "COPYING".
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/smp.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/dca.h>
  23. /* either a kernel change is needed, or we need something like this in kernel */
  24. #ifndef CONFIG_SMP
  25. #include <asm/smp.h>
  26. #undef cpu_physical_id
  27. #define cpu_physical_id(cpu) (cpuid_ebx(1) >> 24)
  28. #endif
  29. #include "dma.h"
  30. #include "registers.h"
  31. /*
  32. * Bit 7 of a tag map entry is the "valid" bit, if it is set then bits 0:6
  33. * contain the bit number of the APIC ID to map into the DCA tag. If the valid
  34. * bit is not set, then the value must be 0 or 1 and defines the bit in the tag.
  35. */
  36. #define DCA_TAG_MAP_VALID 0x80
  37. #define DCA3_TAG_MAP_BIT_TO_INV 0x80
  38. #define DCA3_TAG_MAP_BIT_TO_SEL 0x40
  39. #define DCA3_TAG_MAP_LITERAL_VAL 0x1
  40. #define DCA_TAG_MAP_MASK 0xDF
  41. /* expected tag map bytes for I/OAT ver.2 */
  42. #define DCA2_TAG_MAP_BYTE0 0x80
  43. #define DCA2_TAG_MAP_BYTE1 0x0
  44. #define DCA2_TAG_MAP_BYTE2 0x81
  45. #define DCA2_TAG_MAP_BYTE3 0x82
  46. #define DCA2_TAG_MAP_BYTE4 0x82
  47. /* verify if tag map matches expected values */
  48. static inline int dca2_tag_map_valid(u8 *tag_map)
  49. {
  50. return ((tag_map[0] == DCA2_TAG_MAP_BYTE0) &&
  51. (tag_map[1] == DCA2_TAG_MAP_BYTE1) &&
  52. (tag_map[2] == DCA2_TAG_MAP_BYTE2) &&
  53. (tag_map[3] == DCA2_TAG_MAP_BYTE3) &&
  54. (tag_map[4] == DCA2_TAG_MAP_BYTE4));
  55. }
  56. /*
  57. * "Legacy" DCA systems do not implement the DCA register set in the
  58. * I/OAT device. Software needs direct support for their tag mappings.
  59. */
  60. #define APICID_BIT(x) (DCA_TAG_MAP_VALID | (x))
  61. #define IOAT_TAG_MAP_LEN 8
  62. /* pack PCI B/D/F into a u16 */
  63. static inline u16 dcaid_from_pcidev(struct pci_dev *pci)
  64. {
  65. return (pci->bus->number << 8) | pci->devfn;
  66. }
  67. static int dca_enabled_in_bios(struct pci_dev *pdev)
  68. {
  69. /* CPUID level 9 returns DCA configuration */
  70. /* Bit 0 indicates DCA enabled by the BIOS */
  71. unsigned long cpuid_level_9;
  72. int res;
  73. cpuid_level_9 = cpuid_eax(9);
  74. res = test_bit(0, &cpuid_level_9);
  75. if (!res)
  76. dev_dbg(&pdev->dev, "DCA is disabled in BIOS\n");
  77. return res;
  78. }
  79. int system_has_dca_enabled(struct pci_dev *pdev)
  80. {
  81. if (boot_cpu_has(X86_FEATURE_DCA))
  82. return dca_enabled_in_bios(pdev);
  83. dev_dbg(&pdev->dev, "boot cpu doesn't have X86_FEATURE_DCA\n");
  84. return 0;
  85. }
  86. struct ioat_dca_slot {
  87. struct pci_dev *pdev; /* requester device */
  88. u16 rid; /* requester id, as used by IOAT */
  89. };
  90. #define IOAT_DCA_MAX_REQ 6
  91. #define IOAT3_DCA_MAX_REQ 2
  92. struct ioat_dca_priv {
  93. void __iomem *iobase;
  94. void __iomem *dca_base;
  95. int max_requesters;
  96. int requester_count;
  97. u8 tag_map[IOAT_TAG_MAP_LEN];
  98. struct ioat_dca_slot req_slots[0];
  99. };
  100. static int ioat_dca_dev_managed(struct dca_provider *dca,
  101. struct device *dev)
  102. {
  103. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  104. struct pci_dev *pdev;
  105. int i;
  106. pdev = to_pci_dev(dev);
  107. for (i = 0; i < ioatdca->max_requesters; i++) {
  108. if (ioatdca->req_slots[i].pdev == pdev)
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. static int ioat_dca_add_requester(struct dca_provider *dca, struct device *dev)
  114. {
  115. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  116. struct pci_dev *pdev;
  117. int i;
  118. u16 id;
  119. u16 global_req_table;
  120. /* This implementation only supports PCI-Express */
  121. if (!dev_is_pci(dev))
  122. return -ENODEV;
  123. pdev = to_pci_dev(dev);
  124. id = dcaid_from_pcidev(pdev);
  125. if (ioatdca->requester_count == ioatdca->max_requesters)
  126. return -ENODEV;
  127. for (i = 0; i < ioatdca->max_requesters; i++) {
  128. if (ioatdca->req_slots[i].pdev == NULL) {
  129. /* found an empty slot */
  130. ioatdca->requester_count++;
  131. ioatdca->req_slots[i].pdev = pdev;
  132. ioatdca->req_slots[i].rid = id;
  133. global_req_table =
  134. readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
  135. writel(id | IOAT_DCA_GREQID_VALID,
  136. ioatdca->iobase + global_req_table + (i * 4));
  137. return i;
  138. }
  139. }
  140. /* Error, ioatdma->requester_count is out of whack */
  141. return -EFAULT;
  142. }
  143. static int ioat_dca_remove_requester(struct dca_provider *dca,
  144. struct device *dev)
  145. {
  146. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  147. struct pci_dev *pdev;
  148. int i;
  149. u16 global_req_table;
  150. /* This implementation only supports PCI-Express */
  151. if (!dev_is_pci(dev))
  152. return -ENODEV;
  153. pdev = to_pci_dev(dev);
  154. for (i = 0; i < ioatdca->max_requesters; i++) {
  155. if (ioatdca->req_slots[i].pdev == pdev) {
  156. global_req_table =
  157. readw(ioatdca->dca_base + IOAT3_DCA_GREQID_OFFSET);
  158. writel(0, ioatdca->iobase + global_req_table + (i * 4));
  159. ioatdca->req_slots[i].pdev = NULL;
  160. ioatdca->req_slots[i].rid = 0;
  161. ioatdca->requester_count--;
  162. return i;
  163. }
  164. }
  165. return -ENODEV;
  166. }
  167. static u8 ioat_dca_get_tag(struct dca_provider *dca,
  168. struct device *dev,
  169. int cpu)
  170. {
  171. u8 tag;
  172. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  173. int i, apic_id, bit, value;
  174. u8 entry;
  175. tag = 0;
  176. apic_id = cpu_physical_id(cpu);
  177. for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
  178. entry = ioatdca->tag_map[i];
  179. if (entry & DCA3_TAG_MAP_BIT_TO_SEL) {
  180. bit = entry &
  181. ~(DCA3_TAG_MAP_BIT_TO_SEL | DCA3_TAG_MAP_BIT_TO_INV);
  182. value = (apic_id & (1 << bit)) ? 1 : 0;
  183. } else if (entry & DCA3_TAG_MAP_BIT_TO_INV) {
  184. bit = entry & ~DCA3_TAG_MAP_BIT_TO_INV;
  185. value = (apic_id & (1 << bit)) ? 0 : 1;
  186. } else {
  187. value = (entry & DCA3_TAG_MAP_LITERAL_VAL) ? 1 : 0;
  188. }
  189. tag |= (value << i);
  190. }
  191. return tag;
  192. }
  193. static const struct dca_ops ioat_dca_ops = {
  194. .add_requester = ioat_dca_add_requester,
  195. .remove_requester = ioat_dca_remove_requester,
  196. .get_tag = ioat_dca_get_tag,
  197. .dev_managed = ioat_dca_dev_managed,
  198. };
  199. static int ioat_dca_count_dca_slots(void *iobase, u16 dca_offset)
  200. {
  201. int slots = 0;
  202. u32 req;
  203. u16 global_req_table;
  204. global_req_table = readw(iobase + dca_offset + IOAT3_DCA_GREQID_OFFSET);
  205. if (global_req_table == 0)
  206. return 0;
  207. do {
  208. req = readl(iobase + global_req_table + (slots * sizeof(u32)));
  209. slots++;
  210. } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
  211. return slots;
  212. }
  213. static inline int dca3_tag_map_invalid(u8 *tag_map)
  214. {
  215. /*
  216. * If the tag map is not programmed by the BIOS the default is:
  217. * 0x80 0x80 0x80 0x80 0x80 0x00 0x00 0x00
  218. *
  219. * This an invalid map and will result in only 2 possible tags
  220. * 0x1F and 0x00. 0x00 is an invalid DCA tag so we know that
  221. * this entire definition is invalid.
  222. */
  223. return ((tag_map[0] == DCA_TAG_MAP_VALID) &&
  224. (tag_map[1] == DCA_TAG_MAP_VALID) &&
  225. (tag_map[2] == DCA_TAG_MAP_VALID) &&
  226. (tag_map[3] == DCA_TAG_MAP_VALID) &&
  227. (tag_map[4] == DCA_TAG_MAP_VALID));
  228. }
  229. struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
  230. {
  231. struct dca_provider *dca;
  232. struct ioat_dca_priv *ioatdca;
  233. int slots;
  234. int i;
  235. int err;
  236. u16 dca_offset;
  237. u16 csi_fsb_control;
  238. u16 pcie_control;
  239. u8 bit;
  240. union {
  241. u64 full;
  242. struct {
  243. u32 low;
  244. u32 high;
  245. };
  246. } tag_map;
  247. if (!system_has_dca_enabled(pdev))
  248. return NULL;
  249. dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
  250. if (dca_offset == 0)
  251. return NULL;
  252. slots = ioat_dca_count_dca_slots(iobase, dca_offset);
  253. if (slots == 0)
  254. return NULL;
  255. dca = alloc_dca_provider(&ioat_dca_ops,
  256. sizeof(*ioatdca)
  257. + (sizeof(struct ioat_dca_slot) * slots));
  258. if (!dca)
  259. return NULL;
  260. ioatdca = dca_priv(dca);
  261. ioatdca->iobase = iobase;
  262. ioatdca->dca_base = iobase + dca_offset;
  263. ioatdca->max_requesters = slots;
  264. /* some bios might not know to turn these on */
  265. csi_fsb_control = readw(ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
  266. if ((csi_fsb_control & IOAT3_CSI_CONTROL_PREFETCH) == 0) {
  267. csi_fsb_control |= IOAT3_CSI_CONTROL_PREFETCH;
  268. writew(csi_fsb_control,
  269. ioatdca->dca_base + IOAT3_CSI_CONTROL_OFFSET);
  270. }
  271. pcie_control = readw(ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
  272. if ((pcie_control & IOAT3_PCI_CONTROL_MEMWR) == 0) {
  273. pcie_control |= IOAT3_PCI_CONTROL_MEMWR;
  274. writew(pcie_control,
  275. ioatdca->dca_base + IOAT3_PCI_CONTROL_OFFSET);
  276. }
  277. /* TODO version, compatibility and configuration checks */
  278. /* copy out the APIC to DCA tag map */
  279. tag_map.low =
  280. readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_LOW);
  281. tag_map.high =
  282. readl(ioatdca->dca_base + IOAT3_APICID_TAG_MAP_OFFSET_HIGH);
  283. for (i = 0; i < 8; i++) {
  284. bit = tag_map.full >> (8 * i);
  285. ioatdca->tag_map[i] = bit & DCA_TAG_MAP_MASK;
  286. }
  287. if (dca3_tag_map_invalid(ioatdca->tag_map)) {
  288. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
  289. pr_warn_once("%s %s: APICID_TAG_MAP set incorrectly by BIOS, disabling DCA\n",
  290. dev_driver_string(&pdev->dev),
  291. dev_name(&pdev->dev));
  292. free_dca_provider(dca);
  293. return NULL;
  294. }
  295. err = register_dca_provider(dca, &pdev->dev);
  296. if (err) {
  297. free_dca_provider(dca);
  298. return NULL;
  299. }
  300. return dca;
  301. }