isoch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. u32 tot_bw = 0, tot_n = 0, tot_rq = 0, y_max, rq_isoch, rq_async;
  74. u32 step, rem, rem_isoch, rem_async;
  75. int ret = 0;
  76. /*
  77. * We'll work with an array of isoch_data's (one for each
  78. * device in dev_list) throughout this function.
  79. */
  80. master = kmalloc_array(ndevs, sizeof(*master), GFP_KERNEL);
  81. if (master == 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. pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &mnistat);
  120. master[cdev].maxbw = (mnistat >> 16) & 0xff;
  121. master[cdev].n = (mnistat >> 8) & 0xff;
  122. master[cdev].y = (mnistat >> 6) & 0x3;
  123. master[cdev].dev = cur;
  124. tot_bw += master[cdev].maxbw;
  125. y_max = max(y_max, master[cdev].y);
  126. cdev++;
  127. }
  128. /* Check if this configuration has any chance of working */
  129. if (tot_bw > target.maxbw) {
  130. dev_err(&td->dev, "isochronous bandwidth required "
  131. "by AGP 3.0 devices exceeds that which is supported by "
  132. "the AGP 3.0 bridge!\n");
  133. ret = -ENODEV;
  134. goto free_and_exit;
  135. }
  136. target.y = y_max;
  137. /*
  138. * Write the calculated payload size into the target's NICMD
  139. * register. Doing this directly effects the ISOCH_N value
  140. * in the target's NISTAT register, so we need to do this now
  141. * to get an accurate value for ISOCH_N later.
  142. */
  143. pci_read_config_word(td, bridge->capndx+AGPNICMD, &tnicmd);
  144. tnicmd &= ~(0x3 << 6);
  145. tnicmd |= target.y << 6;
  146. pci_write_config_word(td, bridge->capndx+AGPNICMD, tnicmd);
  147. /* Reread the target's ISOCH_N */
  148. pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  149. target.n = (tnistat >> 8) & 0xff;
  150. /* Calculate the minimum ISOCH_N needed by each master */
  151. for (cdev=0; cdev<ndevs; cdev++) {
  152. master[cdev].y = target.y;
  153. master[cdev].n = master[cdev].maxbw / (master[cdev].y + 1);
  154. tot_n += master[cdev].n;
  155. }
  156. /* Exit if the minimal ISOCH_N allocation among the masters is more
  157. * than the target can handle. */
  158. if (tot_n > target.n) {
  159. dev_err(&td->dev, "number of isochronous "
  160. "transactions per period required by AGP 3.0 devices "
  161. "exceeds that which is supported by the AGP 3.0 "
  162. "bridge!\n");
  163. ret = -ENODEV;
  164. goto free_and_exit;
  165. }
  166. /* Calculate left over ISOCH_N capability in the target. We'll give
  167. * this to the hungriest device (as per the spec) */
  168. rem = target.n - tot_n;
  169. /*
  170. * Calculate the minimum isochronous RQ depth needed by each master.
  171. * Along the way, distribute the extra ISOCH_N capability calculated
  172. * above.
  173. */
  174. for (cdev=0; cdev<ndevs; cdev++) {
  175. /*
  176. * This is a little subtle. If ISOCH_Y > 64B, then ISOCH_Y
  177. * byte isochronous writes will be broken into 64B pieces.
  178. * This means we need to budget more RQ depth to account for
  179. * these kind of writes (each isochronous write is actually
  180. * many writes on the AGP bus).
  181. */
  182. master[cdev].rq = master[cdev].n;
  183. if (master[cdev].y > 0x1)
  184. master[cdev].rq *= (1 << (master[cdev].y - 1));
  185. tot_rq += master[cdev].rq;
  186. }
  187. master[ndevs-1].n += rem;
  188. /* Figure the number of isochronous and asynchronous RQ slots the
  189. * target is providing. */
  190. rq_isoch = (target.y > 0x1) ? target.n * (1 << (target.y - 1)) : target.n;
  191. rq_async = target.rq - rq_isoch;
  192. /* Exit if the minimal RQ needs of the masters exceeds what the target
  193. * can provide. */
  194. if (tot_rq > rq_isoch) {
  195. dev_err(&td->dev, "number of request queue slots "
  196. "required by the isochronous bandwidth requested by "
  197. "AGP 3.0 devices exceeds the number provided by the "
  198. "AGP 3.0 bridge!\n");
  199. ret = -ENODEV;
  200. goto free_and_exit;
  201. }
  202. /* Calculate asynchronous RQ capability in the target (per master) as
  203. * well as the total number of leftover isochronous RQ slots. */
  204. step = rq_async / ndevs;
  205. rem_async = step + (rq_async % ndevs);
  206. rem_isoch = rq_isoch - tot_rq;
  207. /* Distribute the extra RQ slots calculated above and write our
  208. * isochronous settings out to the actual devices. */
  209. for (cdev=0; cdev<ndevs; cdev++) {
  210. cur = master[cdev].dev;
  211. dev = cur->dev;
  212. master[cdev].rq += (cdev == ndevs - 1)
  213. ? (rem_async + rem_isoch) : step;
  214. pci_read_config_word(dev, cur->capndx+AGPNICMD, &mnicmd);
  215. pci_read_config_dword(dev, cur->capndx+AGPCMD, &mcmd);
  216. mnicmd &= ~(0xff << 8);
  217. mnicmd &= ~(0x3 << 6);
  218. mcmd &= ~(0xff << 24);
  219. mnicmd |= master[cdev].n << 8;
  220. mnicmd |= master[cdev].y << 6;
  221. mcmd |= master[cdev].rq << 24;
  222. pci_write_config_dword(dev, cur->capndx+AGPCMD, mcmd);
  223. pci_write_config_word(dev, cur->capndx+AGPNICMD, mnicmd);
  224. }
  225. free_and_exit:
  226. kfree(master);
  227. get_out:
  228. return ret;
  229. }
  230. /*
  231. * This function basically allocates request queue slots among the
  232. * AGP 3.0 systems in nonisochronous nodes. The algorithm is
  233. * pretty stupid, divide the total number of RQ slots provided by the
  234. * target by ndevs. Distribute this many slots to each AGP 3.0 device,
  235. * giving any left over slots to the last device in dev_list.
  236. */
  237. static void agp_3_5_nonisochronous_node_enable(struct agp_bridge_data *bridge,
  238. struct agp_3_5_dev *dev_list, unsigned int ndevs)
  239. {
  240. struct agp_3_5_dev *cur;
  241. struct list_head *head = &dev_list->list, *pos;
  242. u32 tstatus, mcmd;
  243. u32 trq, mrq, rem;
  244. unsigned int cdev = 0;
  245. pci_read_config_dword(bridge->dev, bridge->capndx+AGPSTAT, &tstatus);
  246. trq = (tstatus >> 24) & 0xff;
  247. mrq = trq / ndevs;
  248. rem = mrq + (trq % ndevs);
  249. for (pos=head->next; cdev<ndevs; cdev++, pos=pos->next) {
  250. cur = list_entry(pos, struct agp_3_5_dev, list);
  251. pci_read_config_dword(cur->dev, cur->capndx+AGPCMD, &mcmd);
  252. mcmd &= ~(0xff << 24);
  253. mcmd |= ((cdev == ndevs - 1) ? rem : mrq) << 24;
  254. pci_write_config_dword(cur->dev, cur->capndx+AGPCMD, mcmd);
  255. }
  256. }
  257. /*
  258. * Fully configure and enable an AGP 3.0 host bridge and all the devices
  259. * lying behind it.
  260. */
  261. int agp_3_5_enable(struct agp_bridge_data *bridge)
  262. {
  263. struct pci_dev *td = bridge->dev, *dev = NULL;
  264. u8 mcapndx;
  265. u32 isoch;
  266. u32 tstatus, mstatus, ncapid;
  267. u32 mmajor;
  268. u16 mpstat;
  269. struct agp_3_5_dev *dev_list, *cur;
  270. struct list_head *head, *pos;
  271. unsigned int ndevs = 0;
  272. int ret = 0;
  273. /* Extract some power-on defaults from the target */
  274. pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  275. isoch = (tstatus >> 17) & 0x1;
  276. if (isoch == 0) /* isoch xfers not available, bail out. */
  277. return -ENODEV;
  278. /*
  279. * Allocate a head for our AGP 3.5 device list
  280. * (multiple AGP v3 devices are allowed behind a single bridge).
  281. */
  282. if ((dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL)) == NULL) {
  283. ret = -ENOMEM;
  284. goto get_out;
  285. }
  286. head = &dev_list->list;
  287. INIT_LIST_HEAD(head);
  288. /* Find all AGP devices, and add them to dev_list. */
  289. for_each_pci_dev(dev) {
  290. mcapndx = pci_find_capability(dev, PCI_CAP_ID_AGP);
  291. if (mcapndx == 0)
  292. continue;
  293. switch ((dev->class >>8) & 0xff00) {
  294. case 0x0600: /* Bridge */
  295. /* Skip bridges. We should call this function for each one. */
  296. continue;
  297. case 0x0001: /* Unclassified device */
  298. /* Don't know what this is, but log it for investigation. */
  299. if (mcapndx != 0) {
  300. dev_info(&td->dev, "wacky, found unclassified AGP device %s [%04x/%04x]\n",
  301. pci_name(dev),
  302. dev->vendor, dev->device);
  303. }
  304. continue;
  305. case 0x0300: /* Display controller */
  306. case 0x0400: /* Multimedia controller */
  307. if ((cur = kmalloc(sizeof(*cur), GFP_KERNEL)) == NULL) {
  308. ret = -ENOMEM;
  309. goto free_and_exit;
  310. }
  311. cur->dev = dev;
  312. pos = &cur->list;
  313. list_add(pos, head);
  314. ndevs++;
  315. continue;
  316. default:
  317. continue;
  318. }
  319. }
  320. /*
  321. * Take an initial pass through the devices lying behind our host
  322. * bridge. Make sure each one is actually an AGP 3.0 device, otherwise
  323. * exit with an error message. Along the way store the AGP 3.0
  324. * cap_ptr for each device
  325. */
  326. list_for_each(pos, head) {
  327. cur = list_entry(pos, struct agp_3_5_dev, list);
  328. dev = cur->dev;
  329. pci_read_config_word(dev, PCI_STATUS, &mpstat);
  330. if ((mpstat & PCI_STATUS_CAP_LIST) == 0)
  331. continue;
  332. pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &mcapndx);
  333. if (mcapndx != 0) {
  334. do {
  335. pci_read_config_dword(dev, mcapndx, &ncapid);
  336. if ((ncapid & 0xff) != 2)
  337. mcapndx = (ncapid >> 8) & 0xff;
  338. }
  339. while (((ncapid & 0xff) != 2) && (mcapndx != 0));
  340. }
  341. if (mcapndx == 0) {
  342. dev_err(&td->dev, "woah! Non-AGP device %s on "
  343. "secondary bus of AGP 3.5 bridge!\n",
  344. pci_name(dev));
  345. ret = -ENODEV;
  346. goto free_and_exit;
  347. }
  348. mmajor = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  349. if (mmajor < 3) {
  350. dev_err(&td->dev, "woah! AGP 2.0 device %s on "
  351. "secondary bus of AGP 3.5 bridge operating "
  352. "with AGP 3.0 electricals!\n", pci_name(dev));
  353. ret = -ENODEV;
  354. goto free_and_exit;
  355. }
  356. cur->capndx = mcapndx;
  357. pci_read_config_dword(dev, cur->capndx+AGPSTAT, &mstatus);
  358. if (((mstatus >> 3) & 0x1) == 0) {
  359. dev_err(&td->dev, "woah! AGP 3.x device %s not "
  360. "operating in AGP 3.x mode on secondary bus "
  361. "of AGP 3.5 bridge operating with AGP 3.0 "
  362. "electricals!\n", pci_name(dev));
  363. ret = -ENODEV;
  364. goto free_and_exit;
  365. }
  366. }
  367. /*
  368. * Call functions to divide target resources amongst the AGP 3.0
  369. * masters. This process is dramatically different depending on
  370. * whether isochronous transfers are supported.
  371. */
  372. if (isoch) {
  373. ret = agp_3_5_isochronous_node_enable(bridge, dev_list, ndevs);
  374. if (ret) {
  375. dev_info(&td->dev, "something bad happened setting "
  376. "up isochronous xfers; falling back to "
  377. "non-isochronous xfer mode\n");
  378. } else {
  379. goto free_and_exit;
  380. }
  381. }
  382. agp_3_5_nonisochronous_node_enable(bridge, dev_list, ndevs);
  383. free_and_exit:
  384. /* Be sure to free the dev_list */
  385. for (pos=head->next; pos!=head; ) {
  386. cur = list_entry(pos, struct agp_3_5_dev, list);
  387. pos = pos->next;
  388. kfree(cur);
  389. }
  390. kfree(dev_list);
  391. get_out:
  392. return ret;
  393. }