ioat_dca.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2007 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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/smp.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dca.h>
  27. /* either a kernel change is needed, or we need something like this in kernel */
  28. #ifndef CONFIG_SMP
  29. #include <asm/smp.h>
  30. #undef cpu_physical_id
  31. #define cpu_physical_id(cpu) (cpuid_ebx(1) >> 24)
  32. #endif
  33. #include "ioatdma.h"
  34. #include "ioatdma_registers.h"
  35. /*
  36. * Bit 16 of a tag map entry is the "valid" bit, if it is set then bits 0:15
  37. * contain the bit number of the APIC ID to map into the DCA tag. If the valid
  38. * bit is not set, then the value must be 0 or 1 and defines the bit in the tag.
  39. */
  40. #define DCA_TAG_MAP_VALID 0x80
  41. /*
  42. * "Legacy" DCA systems do not implement the DCA register set in the
  43. * I/OAT device. Software needs direct support for their tag mappings.
  44. */
  45. #define APICID_BIT(x) (DCA_TAG_MAP_VALID | (x))
  46. #define IOAT_TAG_MAP_LEN 8
  47. static u8 ioat_tag_map_BNB[IOAT_TAG_MAP_LEN] = {
  48. 1, APICID_BIT(1), APICID_BIT(2), APICID_BIT(2), };
  49. static u8 ioat_tag_map_SCNB[IOAT_TAG_MAP_LEN] = {
  50. 1, APICID_BIT(1), APICID_BIT(2), APICID_BIT(2), };
  51. static u8 ioat_tag_map_CNB[IOAT_TAG_MAP_LEN] = {
  52. 1, APICID_BIT(1), APICID_BIT(3), APICID_BIT(4), APICID_BIT(2), };
  53. static u8 ioat_tag_map_UNISYS[IOAT_TAG_MAP_LEN] = { 0 };
  54. /* pack PCI B/D/F into a u16 */
  55. static inline u16 dcaid_from_pcidev(struct pci_dev *pci)
  56. {
  57. return (pci->bus->number << 8) | pci->devfn;
  58. }
  59. static int dca_enabled_in_bios(struct pci_dev *pdev)
  60. {
  61. /* CPUID level 9 returns DCA configuration */
  62. /* Bit 0 indicates DCA enabled by the BIOS */
  63. unsigned long cpuid_level_9;
  64. int res;
  65. cpuid_level_9 = cpuid_eax(9);
  66. res = test_bit(0, &cpuid_level_9);
  67. if (!res)
  68. dev_err(&pdev->dev, "DCA is disabled in BIOS\n");
  69. return res;
  70. }
  71. static int system_has_dca_enabled(struct pci_dev *pdev)
  72. {
  73. if (boot_cpu_has(X86_FEATURE_DCA))
  74. return dca_enabled_in_bios(pdev);
  75. dev_err(&pdev->dev, "boot cpu doesn't have X86_FEATURE_DCA\n");
  76. return 0;
  77. }
  78. struct ioat_dca_slot {
  79. struct pci_dev *pdev; /* requester device */
  80. u16 rid; /* requester id, as used by IOAT */
  81. };
  82. #define IOAT_DCA_MAX_REQ 6
  83. struct ioat_dca_priv {
  84. void __iomem *iobase;
  85. void *dca_base;
  86. int max_requesters;
  87. int requester_count;
  88. u8 tag_map[IOAT_TAG_MAP_LEN];
  89. struct ioat_dca_slot req_slots[0];
  90. };
  91. /* 5000 series chipset DCA Port Requester ID Table Entry Format
  92. * [15:8] PCI-Express Bus Number
  93. * [7:3] PCI-Express Device Number
  94. * [2:0] PCI-Express Function Number
  95. *
  96. * 5000 series chipset DCA control register format
  97. * [7:1] Reserved (0)
  98. * [0] Ignore Function Number
  99. */
  100. static int ioat_dca_add_requester(struct dca_provider *dca, struct device *dev)
  101. {
  102. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  103. struct pci_dev *pdev;
  104. int i;
  105. u16 id;
  106. /* This implementation only supports PCI-Express */
  107. if (dev->bus != &pci_bus_type)
  108. return -ENODEV;
  109. pdev = to_pci_dev(dev);
  110. id = dcaid_from_pcidev(pdev);
  111. if (ioatdca->requester_count == ioatdca->max_requesters)
  112. return -ENODEV;
  113. for (i = 0; i < ioatdca->max_requesters; i++) {
  114. if (ioatdca->req_slots[i].pdev == NULL) {
  115. /* found an empty slot */
  116. ioatdca->requester_count++;
  117. ioatdca->req_slots[i].pdev = pdev;
  118. ioatdca->req_slots[i].rid = id;
  119. writew(id, ioatdca->dca_base + (i * 4));
  120. /* make sure the ignore function bit is off */
  121. writeb(0, ioatdca->dca_base + (i * 4) + 2);
  122. return i;
  123. }
  124. }
  125. /* Error, ioatdma->requester_count is out of whack */
  126. return -EFAULT;
  127. }
  128. static int ioat_dca_remove_requester(struct dca_provider *dca,
  129. struct device *dev)
  130. {
  131. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  132. struct pci_dev *pdev;
  133. int i;
  134. /* This implementation only supports PCI-Express */
  135. if (dev->bus != &pci_bus_type)
  136. return -ENODEV;
  137. pdev = to_pci_dev(dev);
  138. for (i = 0; i < ioatdca->max_requesters; i++) {
  139. if (ioatdca->req_slots[i].pdev == pdev) {
  140. writew(0, ioatdca->dca_base + (i * 4));
  141. ioatdca->req_slots[i].pdev = NULL;
  142. ioatdca->req_slots[i].rid = 0;
  143. ioatdca->requester_count--;
  144. return i;
  145. }
  146. }
  147. return -ENODEV;
  148. }
  149. static u8 ioat_dca_get_tag(struct dca_provider *dca, int cpu)
  150. {
  151. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  152. int i, apic_id, bit, value;
  153. u8 entry, tag;
  154. tag = 0;
  155. apic_id = cpu_physical_id(cpu);
  156. for (i = 0; i < IOAT_TAG_MAP_LEN; i++) {
  157. entry = ioatdca->tag_map[i];
  158. if (entry & DCA_TAG_MAP_VALID) {
  159. bit = entry & ~DCA_TAG_MAP_VALID;
  160. value = (apic_id & (1 << bit)) ? 1 : 0;
  161. } else {
  162. value = entry ? 1 : 0;
  163. }
  164. tag |= (value << i);
  165. }
  166. return tag;
  167. }
  168. static struct dca_ops ioat_dca_ops = {
  169. .add_requester = ioat_dca_add_requester,
  170. .remove_requester = ioat_dca_remove_requester,
  171. .get_tag = ioat_dca_get_tag,
  172. };
  173. struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase)
  174. {
  175. struct dca_provider *dca;
  176. struct ioat_dca_priv *ioatdca;
  177. u8 *tag_map = NULL;
  178. int i;
  179. int err;
  180. if (!system_has_dca_enabled(pdev))
  181. return NULL;
  182. /* I/OAT v1 systems must have a known tag_map to support DCA */
  183. switch (pdev->vendor) {
  184. case PCI_VENDOR_ID_INTEL:
  185. switch (pdev->device) {
  186. case PCI_DEVICE_ID_INTEL_IOAT:
  187. tag_map = ioat_tag_map_BNB;
  188. break;
  189. case PCI_DEVICE_ID_INTEL_IOAT_CNB:
  190. tag_map = ioat_tag_map_CNB;
  191. break;
  192. case PCI_DEVICE_ID_INTEL_IOAT_SCNB:
  193. tag_map = ioat_tag_map_SCNB;
  194. break;
  195. }
  196. break;
  197. case PCI_VENDOR_ID_UNISYS:
  198. switch (pdev->device) {
  199. case PCI_DEVICE_ID_UNISYS_DMA_DIRECTOR:
  200. tag_map = ioat_tag_map_UNISYS;
  201. break;
  202. }
  203. break;
  204. }
  205. if (tag_map == NULL)
  206. return NULL;
  207. dca = alloc_dca_provider(&ioat_dca_ops,
  208. sizeof(*ioatdca) +
  209. (sizeof(struct ioat_dca_slot) * IOAT_DCA_MAX_REQ));
  210. if (!dca)
  211. return NULL;
  212. ioatdca = dca_priv(dca);
  213. ioatdca->max_requesters = IOAT_DCA_MAX_REQ;
  214. ioatdca->dca_base = iobase + 0x54;
  215. /* copy over the APIC ID to DCA tag mapping */
  216. for (i = 0; i < IOAT_TAG_MAP_LEN; i++)
  217. ioatdca->tag_map[i] = tag_map[i];
  218. err = register_dca_provider(dca, &pdev->dev);
  219. if (err) {
  220. free_dca_provider(dca);
  221. return NULL;
  222. }
  223. return dca;
  224. }
  225. static int ioat2_dca_add_requester(struct dca_provider *dca, struct device *dev)
  226. {
  227. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  228. struct pci_dev *pdev;
  229. int i;
  230. u16 id;
  231. u16 global_req_table;
  232. /* This implementation only supports PCI-Express */
  233. if (dev->bus != &pci_bus_type)
  234. return -ENODEV;
  235. pdev = to_pci_dev(dev);
  236. id = dcaid_from_pcidev(pdev);
  237. if (ioatdca->requester_count == ioatdca->max_requesters)
  238. return -ENODEV;
  239. for (i = 0; i < ioatdca->max_requesters; i++) {
  240. if (ioatdca->req_slots[i].pdev == NULL) {
  241. /* found an empty slot */
  242. ioatdca->requester_count++;
  243. ioatdca->req_slots[i].pdev = pdev;
  244. ioatdca->req_slots[i].rid = id;
  245. global_req_table =
  246. readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
  247. writel(id | IOAT_DCA_GREQID_VALID,
  248. ioatdca->iobase + global_req_table + (i * 4));
  249. return i;
  250. }
  251. }
  252. /* Error, ioatdma->requester_count is out of whack */
  253. return -EFAULT;
  254. }
  255. static int ioat2_dca_remove_requester(struct dca_provider *dca,
  256. struct device *dev)
  257. {
  258. struct ioat_dca_priv *ioatdca = dca_priv(dca);
  259. struct pci_dev *pdev;
  260. int i;
  261. u16 global_req_table;
  262. /* This implementation only supports PCI-Express */
  263. if (dev->bus != &pci_bus_type)
  264. return -ENODEV;
  265. pdev = to_pci_dev(dev);
  266. for (i = 0; i < ioatdca->max_requesters; i++) {
  267. if (ioatdca->req_slots[i].pdev == pdev) {
  268. global_req_table =
  269. readw(ioatdca->dca_base + IOAT_DCA_GREQID_OFFSET);
  270. writel(0, ioatdca->iobase + global_req_table + (i * 4));
  271. ioatdca->req_slots[i].pdev = NULL;
  272. ioatdca->req_slots[i].rid = 0;
  273. ioatdca->requester_count--;
  274. return i;
  275. }
  276. }
  277. return -ENODEV;
  278. }
  279. static u8 ioat2_dca_get_tag(struct dca_provider *dca, int cpu)
  280. {
  281. u8 tag;
  282. tag = ioat_dca_get_tag(dca, cpu);
  283. tag = (~tag) & 0x1F;
  284. return tag;
  285. }
  286. static struct dca_ops ioat2_dca_ops = {
  287. .add_requester = ioat2_dca_add_requester,
  288. .remove_requester = ioat2_dca_remove_requester,
  289. .get_tag = ioat2_dca_get_tag,
  290. };
  291. static int ioat2_dca_count_dca_slots(void *iobase, u16 dca_offset)
  292. {
  293. int slots = 0;
  294. u32 req;
  295. u16 global_req_table;
  296. global_req_table = readw(iobase + dca_offset + IOAT_DCA_GREQID_OFFSET);
  297. if (global_req_table == 0)
  298. return 0;
  299. do {
  300. req = readl(iobase + global_req_table + (slots * sizeof(u32)));
  301. slots++;
  302. } while ((req & IOAT_DCA_GREQID_LASTID) == 0);
  303. return slots;
  304. }
  305. struct dca_provider *ioat2_dca_init(struct pci_dev *pdev, void __iomem *iobase)
  306. {
  307. struct dca_provider *dca;
  308. struct ioat_dca_priv *ioatdca;
  309. int slots;
  310. int i;
  311. int err;
  312. u32 tag_map;
  313. u16 dca_offset;
  314. u16 csi_fsb_control;
  315. u16 pcie_control;
  316. u8 bit;
  317. if (!system_has_dca_enabled(pdev))
  318. return NULL;
  319. dca_offset = readw(iobase + IOAT_DCAOFFSET_OFFSET);
  320. if (dca_offset == 0)
  321. return NULL;
  322. slots = ioat2_dca_count_dca_slots(iobase, dca_offset);
  323. if (slots == 0)
  324. return NULL;
  325. dca = alloc_dca_provider(&ioat2_dca_ops,
  326. sizeof(*ioatdca)
  327. + (sizeof(struct ioat_dca_slot) * slots));
  328. if (!dca)
  329. return NULL;
  330. ioatdca = dca_priv(dca);
  331. ioatdca->iobase = iobase;
  332. ioatdca->dca_base = iobase + dca_offset;
  333. ioatdca->max_requesters = slots;
  334. /* some bios might not know to turn these on */
  335. csi_fsb_control = readw(ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
  336. if ((csi_fsb_control & IOAT_FSB_CAP_ENABLE_PREFETCH) == 0) {
  337. csi_fsb_control |= IOAT_FSB_CAP_ENABLE_PREFETCH;
  338. writew(csi_fsb_control,
  339. ioatdca->dca_base + IOAT_FSB_CAP_ENABLE_OFFSET);
  340. }
  341. pcie_control = readw(ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
  342. if ((pcie_control & IOAT_PCI_CAP_ENABLE_MEMWR) == 0) {
  343. pcie_control |= IOAT_PCI_CAP_ENABLE_MEMWR;
  344. writew(pcie_control,
  345. ioatdca->dca_base + IOAT_PCI_CAP_ENABLE_OFFSET);
  346. }
  347. /* TODO version, compatibility and configuration checks */
  348. /* copy out the APIC to DCA tag map */
  349. tag_map = readl(ioatdca->dca_base + IOAT_APICID_TAG_MAP_OFFSET);
  350. for (i = 0; i < 5; i++) {
  351. bit = (tag_map >> (4 * i)) & 0x0f;
  352. if (bit < 8)
  353. ioatdca->tag_map[i] = bit | DCA_TAG_MAP_VALID;
  354. else
  355. ioatdca->tag_map[i] = 0;
  356. }
  357. err = register_dca_provider(dca, &pdev->dev);
  358. if (err) {
  359. free_dca_provider(dca);
  360. return NULL;
  361. }
  362. return dca;
  363. }