isoch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Setup routines for AGP 3.5 compliant bridges.
  3. */
  4. #include <linux/list.h>
  5. #include <linux/pci.h>
  6. #include <linux/agp_backend.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include "agp.h"
  10. /* Generic AGP 3.5 enabling routines */
  11. struct agp_3_5_dev {
  12. struct list_head list;
  13. u8 capndx;
  14. u32 maxbw;
  15. struct pci_dev *dev;
  16. };
  17. static void agp_3_5_dev_list_insert(struct list_head *head, struct list_head *new)
  18. {
  19. struct agp_3_5_dev *cur, *n = list_entry(new, struct agp_3_5_dev, list);
  20. struct list_head *pos;
  21. list_for_each(pos, head) {
  22. cur = list_entry(pos, struct agp_3_5_dev, list);
  23. if (cur->maxbw > n->maxbw)
  24. break;
  25. }
  26. list_add_tail(new, pos);
  27. }
  28. static void agp_3_5_dev_list_sort(struct agp_3_5_dev *list, unsigned int ndevs)
  29. {
  30. struct agp_3_5_dev *cur;
  31. struct pci_dev *dev;
  32. struct list_head *pos, *tmp, *head = &list->list, *start = head->next;
  33. u32 nistat;
  34. INIT_LIST_HEAD(head);
  35. for (pos=start; pos!=head; ) {
  36. cur = list_entry(pos, struct agp_3_5_dev, list);
  37. dev = cur->dev;
  38. pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &nistat);
  39. cur->maxbw = (nistat >> 16) & 0xff;
  40. tmp = pos;
  41. pos = pos->next;
  42. agp_3_5_dev_list_insert(head, tmp);
  43. }
  44. }
  45. /*
  46. * Initialize all isochronous transfer parameters for an AGP 3.0
  47. * node (i.e. a host bridge in combination with the adapters
  48. * lying behind it...)
  49. */
  50. static int agp_3_5_isochronous_node_enable(struct agp_bridge_data *bridge,
  51. struct agp_3_5_dev *dev_list, unsigned int ndevs)
  52. {
  53. /*
  54. * Convenience structure to make the calculations clearer
  55. * here. The field names come straight from the AGP 3.0 spec.
  56. */
  57. struct isoch_data {
  58. u32 maxbw;
  59. u32 n;
  60. u32 y;
  61. u32 l;
  62. u32 rq;
  63. struct agp_3_5_dev *dev;
  64. };
  65. struct pci_dev *td = bridge->dev, *dev;
  66. struct list_head *head = &dev_list->list, *pos;
  67. struct agp_3_5_dev *cur;
  68. struct isoch_data *master, target;
  69. unsigned int cdev = 0;
  70. u32 mnistat, tnistat, tstatus, mcmd;
  71. u16 tnicmd, mnicmd;
  72. u8 mcapndx;
  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. if ((master = kmalloc(ndevs * sizeof(*master), GFP_KERNEL)) == NULL) {
  81. ret = -ENOMEM;
  82. goto get_out;
  83. }
  84. /*
  85. * Sort the device list by maxbw. We need to do this because the
  86. * spec suggests that the devices with the smallest requirements
  87. * have their resources allocated first, with all remaining resources
  88. * falling to the device with the largest requirement.
  89. *
  90. * We don't exactly do this, we divide target resources by ndevs
  91. * and split them amongst the AGP 3.0 devices. The remainder of such
  92. * division operations are dropped on the last device, sort of like
  93. * the spec mentions it should be done.
  94. *
  95. * We can't do this sort when we initially construct the dev_list
  96. * because we don't know until this function whether isochronous
  97. * transfers are enabled and consequently whether maxbw will mean
  98. * anything.
  99. */
  100. agp_3_5_dev_list_sort(dev_list, ndevs);
  101. pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  102. pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  103. /* Extract power-on defaults from the target */
  104. target.maxbw = (tnistat >> 16) & 0xff;
  105. target.n = (tnistat >> 8) & 0xff;
  106. target.y = (tnistat >> 6) & 0x3;
  107. target.l = (tnistat >> 3) & 0x7;
  108. target.rq = (tstatus >> 24) & 0xff;
  109. y_max = target.y;
  110. /*
  111. * Extract power-on defaults for each device in dev_list. Along
  112. * the way, calculate the total isochronous bandwidth required
  113. * by these devices and the largest requested payload size.
  114. */
  115. list_for_each(pos, head) {
  116. cur = list_entry(pos, struct agp_3_5_dev, list);
  117. dev = cur->dev;
  118. mcapndx = cur->capndx;
  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. mcapndx = cur->capndx;
  213. master[cdev].rq += (cdev == ndevs - 1)
  214. ? (rem_async + rem_isoch) : step;
  215. pci_read_config_word(dev, cur->capndx+AGPNICMD, &mnicmd);
  216. pci_read_config_dword(dev, cur->capndx+AGPCMD, &mcmd);
  217. mnicmd &= ~(0xff << 8);
  218. mnicmd &= ~(0x3 << 6);
  219. mcmd &= ~(0xff << 24);
  220. mnicmd |= master[cdev].n << 8;
  221. mnicmd |= master[cdev].y << 6;
  222. mcmd |= master[cdev].rq << 24;
  223. pci_write_config_dword(dev, cur->capndx+AGPCMD, mcmd);
  224. pci_write_config_word(dev, cur->capndx+AGPNICMD, mnicmd);
  225. }
  226. free_and_exit:
  227. kfree(master);
  228. get_out:
  229. return ret;
  230. }
  231. /*
  232. * This function basically allocates request queue slots among the
  233. * AGP 3.0 systems in nonisochronous nodes. The algorithm is
  234. * pretty stupid, divide the total number of RQ slots provided by the
  235. * target by ndevs. Distribute this many slots to each AGP 3.0 device,
  236. * giving any left over slots to the last device in dev_list.
  237. */
  238. static void agp_3_5_nonisochronous_node_enable(struct agp_bridge_data *bridge,
  239. struct agp_3_5_dev *dev_list, unsigned int ndevs)
  240. {
  241. struct agp_3_5_dev *cur;
  242. struct list_head *head = &dev_list->list, *pos;
  243. u32 tstatus, mcmd;
  244. u32 trq, mrq, rem;
  245. unsigned int cdev = 0;
  246. pci_read_config_dword(bridge->dev, bridge->capndx+AGPSTAT, &tstatus);
  247. trq = (tstatus >> 24) & 0xff;
  248. mrq = trq / ndevs;
  249. rem = mrq + (trq % ndevs);
  250. for (pos=head->next; cdev<ndevs; cdev++, pos=pos->next) {
  251. cur = list_entry(pos, struct agp_3_5_dev, list);
  252. pci_read_config_dword(cur->dev, cur->capndx+AGPCMD, &mcmd);
  253. mcmd &= ~(0xff << 24);
  254. mcmd |= ((cdev == ndevs - 1) ? rem : mrq) << 24;
  255. pci_write_config_dword(cur->dev, cur->capndx+AGPCMD, mcmd);
  256. }
  257. }
  258. /*
  259. * Fully configure and enable an AGP 3.0 host bridge and all the devices
  260. * lying behind it.
  261. */
  262. int agp_3_5_enable(struct agp_bridge_data *bridge)
  263. {
  264. struct pci_dev *td = bridge->dev, *dev = NULL;
  265. u8 mcapndx;
  266. u32 isoch, arqsz;
  267. u32 tstatus, mstatus, ncapid;
  268. u32 mmajor;
  269. u16 mpstat;
  270. struct agp_3_5_dev *dev_list, *cur;
  271. struct list_head *head, *pos;
  272. unsigned int ndevs = 0;
  273. int ret = 0;
  274. /* Extract some power-on defaults from the target */
  275. pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  276. isoch = (tstatus >> 17) & 0x1;
  277. if (isoch == 0) /* isoch xfers not available, bail out. */
  278. return -ENODEV;
  279. arqsz = (tstatus >> 13) & 0x7;
  280. /*
  281. * Allocate a head for our AGP 3.5 device list
  282. * (multiple AGP v3 devices are allowed behind a single bridge).
  283. */
  284. if ((dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL)) == NULL) {
  285. ret = -ENOMEM;
  286. goto get_out;
  287. }
  288. head = &dev_list->list;
  289. INIT_LIST_HEAD(head);
  290. /* Find all AGP devices, and add them to dev_list. */
  291. for_each_pci_dev(dev) {
  292. mcapndx = pci_find_capability(dev, PCI_CAP_ID_AGP);
  293. if (mcapndx == 0)
  294. continue;
  295. switch ((dev->class >>8) & 0xff00) {
  296. case 0x0600: /* Bridge */
  297. /* Skip bridges. We should call this function for each one. */
  298. continue;
  299. case 0x0001: /* Unclassified device */
  300. /* Don't know what this is, but log it for investigation. */
  301. if (mcapndx != 0) {
  302. dev_info(&td->dev, "wacky, found unclassified AGP device %s [%04x/%04x]\n",
  303. pci_name(dev),
  304. dev->vendor, dev->device);
  305. }
  306. continue;
  307. case 0x0300: /* Display controller */
  308. case 0x0400: /* Multimedia controller */
  309. if ((cur = kmalloc(sizeof(*cur), GFP_KERNEL)) == NULL) {
  310. ret = -ENOMEM;
  311. goto free_and_exit;
  312. }
  313. cur->dev = dev;
  314. pos = &cur->list;
  315. list_add(pos, head);
  316. ndevs++;
  317. continue;
  318. default:
  319. continue;
  320. }
  321. }
  322. /*
  323. * Take an initial pass through the devices lying behind our host
  324. * bridge. Make sure each one is actually an AGP 3.0 device, otherwise
  325. * exit with an error message. Along the way store the AGP 3.0
  326. * cap_ptr for each device
  327. */
  328. list_for_each(pos, head) {
  329. cur = list_entry(pos, struct agp_3_5_dev, list);
  330. dev = cur->dev;
  331. pci_read_config_word(dev, PCI_STATUS, &mpstat);
  332. if ((mpstat & PCI_STATUS_CAP_LIST) == 0)
  333. continue;
  334. pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &mcapndx);
  335. if (mcapndx != 0) {
  336. do {
  337. pci_read_config_dword(dev, mcapndx, &ncapid);
  338. if ((ncapid & 0xff) != 2)
  339. mcapndx = (ncapid >> 8) & 0xff;
  340. }
  341. while (((ncapid & 0xff) != 2) && (mcapndx != 0));
  342. }
  343. if (mcapndx == 0) {
  344. dev_err(&td->dev, "woah! Non-AGP device %s on "
  345. "secondary bus of AGP 3.5 bridge!\n",
  346. pci_name(dev));
  347. ret = -ENODEV;
  348. goto free_and_exit;
  349. }
  350. mmajor = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  351. if (mmajor < 3) {
  352. dev_err(&td->dev, "woah! AGP 2.0 device %s on "
  353. "secondary bus of AGP 3.5 bridge operating "
  354. "with AGP 3.0 electricals!\n", pci_name(dev));
  355. ret = -ENODEV;
  356. goto free_and_exit;
  357. }
  358. cur->capndx = mcapndx;
  359. pci_read_config_dword(dev, cur->capndx+AGPSTAT, &mstatus);
  360. if (((mstatus >> 3) & 0x1) == 0) {
  361. dev_err(&td->dev, "woah! AGP 3.x device %s not "
  362. "operating in AGP 3.x mode on secondary bus "
  363. "of AGP 3.5 bridge operating with AGP 3.0 "
  364. "electricals!\n", pci_name(dev));
  365. ret = -ENODEV;
  366. goto free_and_exit;
  367. }
  368. }
  369. /*
  370. * Call functions to divide target resources amongst the AGP 3.0
  371. * masters. This process is dramatically different depending on
  372. * whether isochronous transfers are supported.
  373. */
  374. if (isoch) {
  375. ret = agp_3_5_isochronous_node_enable(bridge, dev_list, ndevs);
  376. if (ret) {
  377. dev_info(&td->dev, "something bad happened setting "
  378. "up isochronous xfers; falling back to "
  379. "non-isochronous xfer mode\n");
  380. } else {
  381. goto free_and_exit;
  382. }
  383. }
  384. agp_3_5_nonisochronous_node_enable(bridge, dev_list, ndevs);
  385. free_and_exit:
  386. /* Be sure to free the dev_list */
  387. for (pos=head->next; pos!=head; ) {
  388. cur = list_entry(pos, struct agp_3_5_dev, list);
  389. pos = pos->next;
  390. kfree(cur);
  391. }
  392. kfree(dev_list);
  393. get_out:
  394. return ret;
  395. }