isoch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Setup routines for AGP 3.5 compliant bridges.
  4. */
  5. #include <linux/list.h>
  6. #include <linux/pci.h>
  7. #include <linux/agp_backend.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include "agp.h"
  11. /* Generic AGP 3.5 enabling routines */
  12. struct agp_3_5_dev {
  13. struct list_head list;
  14. u8 capndx;
  15. u32 maxbw;
  16. struct pci_dev *dev;
  17. };
  18. static void agp_3_5_dev_list_insert(struct list_head *head, struct list_head *new)
  19. {
  20. struct agp_3_5_dev *cur, *n = list_entry(new, struct agp_3_5_dev, list);
  21. struct list_head *pos;
  22. list_for_each(pos, head) {
  23. cur = list_entry(pos, struct agp_3_5_dev, list);
  24. if (cur->maxbw > n->maxbw)
  25. break;
  26. }
  27. list_add_tail(new, pos);
  28. }
  29. static void agp_3_5_dev_list_sort(struct agp_3_5_dev *list, unsigned int ndevs)
  30. {
  31. struct agp_3_5_dev *cur;
  32. struct pci_dev *dev;
  33. struct list_head *pos, *tmp, *head = &list->list, *start = head->next;
  34. u32 nistat;
  35. INIT_LIST_HEAD(head);
  36. for (pos=start; pos!=head; ) {
  37. cur = list_entry(pos, struct agp_3_5_dev, list);
  38. dev = cur->dev;
  39. pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &nistat);
  40. cur->maxbw = (nistat >> 16) & 0xff;
  41. tmp = pos;
  42. pos = pos->next;
  43. agp_3_5_dev_list_insert(head, tmp);
  44. }
  45. }
  46. /*
  47. * Initialize all isochronous transfer parameters for an AGP 3.0
  48. * node (i.e. a host bridge in combination with the adapters
  49. * lying behind it...)
  50. */
  51. static int agp_3_5_isochronous_node_enable(struct agp_bridge_data *bridge,
  52. struct agp_3_5_dev *dev_list, unsigned int ndevs)
  53. {
  54. /*
  55. * Convenience structure to make the calculations clearer
  56. * here. The field names come straight from the AGP 3.0 spec.
  57. */
  58. struct isoch_data {
  59. u32 maxbw;
  60. u32 n;
  61. u32 y;
  62. u32 l;
  63. u32 rq;
  64. struct agp_3_5_dev *dev;
  65. };
  66. struct pci_dev *td = bridge->dev, *dev;
  67. struct list_head *head = &dev_list->list, *pos;
  68. struct agp_3_5_dev *cur;
  69. struct isoch_data *master, target;
  70. unsigned int cdev = 0;
  71. u32 mnistat, tnistat, tstatus, mcmd;
  72. u16 tnicmd, mnicmd;
  73. u8 mcapndx;
  74. u32 tot_bw = 0, tot_n = 0, tot_rq = 0, y_max, rq_isoch, rq_async;
  75. u32 step, rem, rem_isoch, rem_async;
  76. int ret = 0;
  77. /*
  78. * We'll work with an array of isoch_data's (one for each
  79. * device in dev_list) throughout this function.
  80. */
  81. if ((master = kmalloc(ndevs * sizeof(*master), GFP_KERNEL)) == NULL) {
  82. ret = -ENOMEM;
  83. goto get_out;
  84. }
  85. /*
  86. * Sort the device list by maxbw. We need to do this because the
  87. * spec suggests that the devices with the smallest requirements
  88. * have their resources allocated first, with all remaining resources
  89. * falling to the device with the largest requirement.
  90. *
  91. * We don't exactly do this, we divide target resources by ndevs
  92. * and split them amongst the AGP 3.0 devices. The remainder of such
  93. * division operations are dropped on the last device, sort of like
  94. * the spec mentions it should be done.
  95. *
  96. * We can't do this sort when we initially construct the dev_list
  97. * because we don't know until this function whether isochronous
  98. * transfers are enabled and consequently whether maxbw will mean
  99. * anything.
  100. */
  101. agp_3_5_dev_list_sort(dev_list, ndevs);
  102. pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  103. pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  104. /* Extract power-on defaults from the target */
  105. target.maxbw = (tnistat >> 16) & 0xff;
  106. target.n = (tnistat >> 8) & 0xff;
  107. target.y = (tnistat >> 6) & 0x3;
  108. target.l = (tnistat >> 3) & 0x7;
  109. target.rq = (tstatus >> 24) & 0xff;
  110. y_max = target.y;
  111. /*
  112. * Extract power-on defaults for each device in dev_list. Along
  113. * the way, calculate the total isochronous bandwidth required
  114. * by these devices and the largest requested payload size.
  115. */
  116. list_for_each(pos, head) {
  117. cur = list_entry(pos, struct agp_3_5_dev, list);
  118. dev = cur->dev;
  119. mcapndx = cur->capndx;
  120. pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &mnistat);
  121. master[cdev].maxbw = (mnistat >> 16) & 0xff;
  122. master[cdev].n = (mnistat >> 8) & 0xff;
  123. master[cdev].y = (mnistat >> 6) & 0x3;
  124. master[cdev].dev = cur;
  125. tot_bw += master[cdev].maxbw;
  126. y_max = max(y_max, master[cdev].y);
  127. cdev++;
  128. }
  129. /* Check if this configuration has any chance of working */
  130. if (tot_bw > target.maxbw) {
  131. dev_err(&td->dev, "isochronous bandwidth required "
  132. "by AGP 3.0 devices exceeds that which is supported by "
  133. "the AGP 3.0 bridge!\n");
  134. ret = -ENODEV;
  135. goto free_and_exit;
  136. }
  137. target.y = y_max;
  138. /*
  139. * Write the calculated payload size into the target's NICMD
  140. * register. Doing this directly effects the ISOCH_N value
  141. * in the target's NISTAT register, so we need to do this now
  142. * to get an accurate value for ISOCH_N later.
  143. */
  144. pci_read_config_word(td, bridge->capndx+AGPNICMD, &tnicmd);
  145. tnicmd &= ~(0x3 << 6);
  146. tnicmd |= target.y << 6;
  147. pci_write_config_word(td, bridge->capndx+AGPNICMD, tnicmd);
  148. /* Reread the target's ISOCH_N */
  149. pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  150. target.n = (tnistat >> 8) & 0xff;
  151. /* Calculate the minimum ISOCH_N needed by each master */
  152. for (cdev=0; cdev<ndevs; cdev++) {
  153. master[cdev].y = target.y;
  154. master[cdev].n = master[cdev].maxbw / (master[cdev].y + 1);
  155. tot_n += master[cdev].n;
  156. }
  157. /* Exit if the minimal ISOCH_N allocation among the masters is more
  158. * than the target can handle. */
  159. if (tot_n > target.n) {
  160. dev_err(&td->dev, "number of isochronous "
  161. "transactions per period required by AGP 3.0 devices "
  162. "exceeds that which is supported by the AGP 3.0 "
  163. "bridge!\n");
  164. ret = -ENODEV;
  165. goto free_and_exit;
  166. }
  167. /* Calculate left over ISOCH_N capability in the target. We'll give
  168. * this to the hungriest device (as per the spec) */
  169. rem = target.n - tot_n;
  170. /*
  171. * Calculate the minimum isochronous RQ depth needed by each master.
  172. * Along the way, distribute the extra ISOCH_N capability calculated
  173. * above.
  174. */
  175. for (cdev=0; cdev<ndevs; cdev++) {
  176. /*
  177. * This is a little subtle. If ISOCH_Y > 64B, then ISOCH_Y
  178. * byte isochronous writes will be broken into 64B pieces.
  179. * This means we need to budget more RQ depth to account for
  180. * these kind of writes (each isochronous write is actually
  181. * many writes on the AGP bus).
  182. */
  183. master[cdev].rq = master[cdev].n;
  184. if (master[cdev].y > 0x1)
  185. master[cdev].rq *= (1 << (master[cdev].y - 1));
  186. tot_rq += master[cdev].rq;
  187. }
  188. master[ndevs-1].n += rem;
  189. /* Figure the number of isochronous and asynchronous RQ slots the
  190. * target is providing. */
  191. rq_isoch = (target.y > 0x1) ? target.n * (1 << (target.y - 1)) : target.n;
  192. rq_async = target.rq - rq_isoch;
  193. /* Exit if the minimal RQ needs of the masters exceeds what the target
  194. * can provide. */
  195. if (tot_rq > rq_isoch) {
  196. dev_err(&td->dev, "number of request queue slots "
  197. "required by the isochronous bandwidth requested by "
  198. "AGP 3.0 devices exceeds the number provided by the "
  199. "AGP 3.0 bridge!\n");
  200. ret = -ENODEV;
  201. goto free_and_exit;
  202. }
  203. /* Calculate asynchronous RQ capability in the target (per master) as
  204. * well as the total number of leftover isochronous RQ slots. */
  205. step = rq_async / ndevs;
  206. rem_async = step + (rq_async % ndevs);
  207. rem_isoch = rq_isoch - tot_rq;
  208. /* Distribute the extra RQ slots calculated above and write our
  209. * isochronous settings out to the actual devices. */
  210. for (cdev=0; cdev<ndevs; cdev++) {
  211. cur = master[cdev].dev;
  212. dev = cur->dev;
  213. mcapndx = cur->capndx;
  214. master[cdev].rq += (cdev == ndevs - 1)
  215. ? (rem_async + rem_isoch) : step;
  216. pci_read_config_word(dev, cur->capndx+AGPNICMD, &mnicmd);
  217. pci_read_config_dword(dev, cur->capndx+AGPCMD, &mcmd);
  218. mnicmd &= ~(0xff << 8);
  219. mnicmd &= ~(0x3 << 6);
  220. mcmd &= ~(0xff << 24);
  221. mnicmd |= master[cdev].n << 8;
  222. mnicmd |= master[cdev].y << 6;
  223. mcmd |= master[cdev].rq << 24;
  224. pci_write_config_dword(dev, cur->capndx+AGPCMD, mcmd);
  225. pci_write_config_word(dev, cur->capndx+AGPNICMD, mnicmd);
  226. }
  227. free_and_exit:
  228. kfree(master);
  229. get_out:
  230. return ret;
  231. }
  232. /*
  233. * This function basically allocates request queue slots among the
  234. * AGP 3.0 systems in nonisochronous nodes. The algorithm is
  235. * pretty stupid, divide the total number of RQ slots provided by the
  236. * target by ndevs. Distribute this many slots to each AGP 3.0 device,
  237. * giving any left over slots to the last device in dev_list.
  238. */
  239. static void agp_3_5_nonisochronous_node_enable(struct agp_bridge_data *bridge,
  240. struct agp_3_5_dev *dev_list, unsigned int ndevs)
  241. {
  242. struct agp_3_5_dev *cur;
  243. struct list_head *head = &dev_list->list, *pos;
  244. u32 tstatus, mcmd;
  245. u32 trq, mrq, rem;
  246. unsigned int cdev = 0;
  247. pci_read_config_dword(bridge->dev, bridge->capndx+AGPSTAT, &tstatus);
  248. trq = (tstatus >> 24) & 0xff;
  249. mrq = trq / ndevs;
  250. rem = mrq + (trq % ndevs);
  251. for (pos=head->next; cdev<ndevs; cdev++, pos=pos->next) {
  252. cur = list_entry(pos, struct agp_3_5_dev, list);
  253. pci_read_config_dword(cur->dev, cur->capndx+AGPCMD, &mcmd);
  254. mcmd &= ~(0xff << 24);
  255. mcmd |= ((cdev == ndevs - 1) ? rem : mrq) << 24;
  256. pci_write_config_dword(cur->dev, cur->capndx+AGPCMD, mcmd);
  257. }
  258. }
  259. /*
  260. * Fully configure and enable an AGP 3.0 host bridge and all the devices
  261. * lying behind it.
  262. */
  263. int agp_3_5_enable(struct agp_bridge_data *bridge)
  264. {
  265. struct pci_dev *td = bridge->dev, *dev = NULL;
  266. u8 mcapndx;
  267. u32 isoch, arqsz;
  268. u32 tstatus, mstatus, ncapid;
  269. u32 mmajor;
  270. u16 mpstat;
  271. struct agp_3_5_dev *dev_list, *cur;
  272. struct list_head *head, *pos;
  273. unsigned int ndevs = 0;
  274. int ret = 0;
  275. /* Extract some power-on defaults from the target */
  276. pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  277. isoch = (tstatus >> 17) & 0x1;
  278. if (isoch == 0) /* isoch xfers not available, bail out. */
  279. return -ENODEV;
  280. arqsz = (tstatus >> 13) & 0x7;
  281. /*
  282. * Allocate a head for our AGP 3.5 device list
  283. * (multiple AGP v3 devices are allowed behind a single bridge).
  284. */
  285. if ((dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL)) == NULL) {
  286. ret = -ENOMEM;
  287. goto get_out;
  288. }
  289. head = &dev_list->list;
  290. INIT_LIST_HEAD(head);
  291. /* Find all AGP devices, and add them to dev_list. */
  292. for_each_pci_dev(dev) {
  293. mcapndx = pci_find_capability(dev, PCI_CAP_ID_AGP);
  294. if (mcapndx == 0)
  295. continue;
  296. switch ((dev->class >>8) & 0xff00) {
  297. case 0x0600: /* Bridge */
  298. /* Skip bridges. We should call this function for each one. */
  299. continue;
  300. case 0x0001: /* Unclassified device */
  301. /* Don't know what this is, but log it for investigation. */
  302. if (mcapndx != 0) {
  303. dev_info(&td->dev, "wacky, found unclassified AGP device %s [%04x/%04x]\n",
  304. pci_name(dev),
  305. dev->vendor, dev->device);
  306. }
  307. continue;
  308. case 0x0300: /* Display controller */
  309. case 0x0400: /* Multimedia controller */
  310. if ((cur = kmalloc(sizeof(*cur), GFP_KERNEL)) == NULL) {
  311. ret = -ENOMEM;
  312. goto free_and_exit;
  313. }
  314. cur->dev = dev;
  315. pos = &cur->list;
  316. list_add(pos, head);
  317. ndevs++;
  318. continue;
  319. default:
  320. continue;
  321. }
  322. }
  323. /*
  324. * Take an initial pass through the devices lying behind our host
  325. * bridge. Make sure each one is actually an AGP 3.0 device, otherwise
  326. * exit with an error message. Along the way store the AGP 3.0
  327. * cap_ptr for each device
  328. */
  329. list_for_each(pos, head) {
  330. cur = list_entry(pos, struct agp_3_5_dev, list);
  331. dev = cur->dev;
  332. pci_read_config_word(dev, PCI_STATUS, &mpstat);
  333. if ((mpstat & PCI_STATUS_CAP_LIST) == 0)
  334. continue;
  335. pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &mcapndx);
  336. if (mcapndx != 0) {
  337. do {
  338. pci_read_config_dword(dev, mcapndx, &ncapid);
  339. if ((ncapid & 0xff) != 2)
  340. mcapndx = (ncapid >> 8) & 0xff;
  341. }
  342. while (((ncapid & 0xff) != 2) && (mcapndx != 0));
  343. }
  344. if (mcapndx == 0) {
  345. dev_err(&td->dev, "woah! Non-AGP device %s on "
  346. "secondary bus of AGP 3.5 bridge!\n",
  347. pci_name(dev));
  348. ret = -ENODEV;
  349. goto free_and_exit;
  350. }
  351. mmajor = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  352. if (mmajor < 3) {
  353. dev_err(&td->dev, "woah! AGP 2.0 device %s on "
  354. "secondary bus of AGP 3.5 bridge operating "
  355. "with AGP 3.0 electricals!\n", pci_name(dev));
  356. ret = -ENODEV;
  357. goto free_and_exit;
  358. }
  359. cur->capndx = mcapndx;
  360. pci_read_config_dword(dev, cur->capndx+AGPSTAT, &mstatus);
  361. if (((mstatus >> 3) & 0x1) == 0) {
  362. dev_err(&td->dev, "woah! AGP 3.x device %s not "
  363. "operating in AGP 3.x mode on secondary bus "
  364. "of AGP 3.5 bridge operating with AGP 3.0 "
  365. "electricals!\n", pci_name(dev));
  366. ret = -ENODEV;
  367. goto free_and_exit;
  368. }
  369. }
  370. /*
  371. * Call functions to divide target resources amongst the AGP 3.0
  372. * masters. This process is dramatically different depending on
  373. * whether isochronous transfers are supported.
  374. */
  375. if (isoch) {
  376. ret = agp_3_5_isochronous_node_enable(bridge, dev_list, ndevs);
  377. if (ret) {
  378. dev_info(&td->dev, "something bad happened setting "
  379. "up isochronous xfers; falling back to "
  380. "non-isochronous xfer mode\n");
  381. } else {
  382. goto free_and_exit;
  383. }
  384. }
  385. agp_3_5_nonisochronous_node_enable(bridge, dev_list, ndevs);
  386. free_and_exit:
  387. /* Be sure to free the dev_list */
  388. for (pos=head->next; pos!=head; ) {
  389. cur = list_entry(pos, struct agp_3_5_dev, list);
  390. pos = pos->next;
  391. kfree(cur);
  392. }
  393. kfree(dev_list);
  394. get_out:
  395. return ret;
  396. }