pci_common.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
  2. * pci_common.c: PCI controller common support.
  3. *
  4. * Copyright (C) 1999 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/string.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <asm/pbm.h>
  10. /* Fix self device of BUS and hook it into BUS->self.
  11. * The pci_scan_bus does not do this for the host bridge.
  12. */
  13. void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
  14. {
  15. struct pci_dev *pdev;
  16. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  17. if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
  18. pbus->self = pdev;
  19. return;
  20. }
  21. }
  22. prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
  23. prom_halt();
  24. }
  25. /* Find the OBP PROM device tree node for a PCI device.
  26. * Return zero if not found.
  27. */
  28. static int __init find_device_prom_node(struct pci_pbm_info *pbm,
  29. struct pci_dev *pdev,
  30. int bus_prom_node,
  31. struct linux_prom_pci_registers *pregs,
  32. int *nregs)
  33. {
  34. int node;
  35. *nregs = 0;
  36. /*
  37. * Return the PBM's PROM node in case we are it's PCI device,
  38. * as the PBM's reg property is different to standard PCI reg
  39. * properties. We would delete this device entry otherwise,
  40. * which confuses XFree86's device probing...
  41. */
  42. if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
  43. (pdev->vendor == PCI_VENDOR_ID_SUN) &&
  44. (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
  45. pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
  46. pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
  47. pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
  48. pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
  49. return bus_prom_node;
  50. node = prom_getchild(bus_prom_node);
  51. while (node != 0) {
  52. int err = prom_getproperty(node, "reg",
  53. (char *)pregs,
  54. sizeof(*pregs) * PROMREG_MAX);
  55. if (err == 0 || err == -1)
  56. goto do_next_sibling;
  57. if (((pregs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
  58. *nregs = err / sizeof(*pregs);
  59. return node;
  60. }
  61. do_next_sibling:
  62. node = prom_getsibling(node);
  63. }
  64. return 0;
  65. }
  66. /* Older versions of OBP on PCI systems encode 64-bit MEM
  67. * space assignments incorrectly, this fixes them up. We also
  68. * take the opportunity here to hide other kinds of bogus
  69. * assignments.
  70. */
  71. static void __init fixup_obp_assignments(struct pci_dev *pdev,
  72. struct pcidev_cookie *pcp)
  73. {
  74. int i;
  75. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  76. (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
  77. pdev->device == PCI_DEVICE_ID_AL_M1533)) {
  78. int i;
  79. /* Zap all of the normal resources, they are
  80. * meaningless and generate bogus resource collision
  81. * messages. This is OpenBoot's ill-fated attempt to
  82. * represent the implicit resources that these devices
  83. * have.
  84. */
  85. pcp->num_prom_assignments = 0;
  86. for (i = 0; i < 6; i++) {
  87. pdev->resource[i].start =
  88. pdev->resource[i].end =
  89. pdev->resource[i].flags = 0;
  90. }
  91. pdev->resource[PCI_ROM_RESOURCE].start =
  92. pdev->resource[PCI_ROM_RESOURCE].end =
  93. pdev->resource[PCI_ROM_RESOURCE].flags = 0;
  94. return;
  95. }
  96. for (i = 0; i < pcp->num_prom_assignments; i++) {
  97. struct linux_prom_pci_registers *ap;
  98. int space;
  99. ap = &pcp->prom_assignments[i];
  100. space = ap->phys_hi >> 24;
  101. if ((space & 0x3) == 2 &&
  102. (space & 0x4) != 0) {
  103. ap->phys_hi &= ~(0x7 << 24);
  104. ap->phys_hi |= 0x3 << 24;
  105. }
  106. }
  107. }
  108. /* Fill in the PCI device cookie sysdata for the given
  109. * PCI device. This cookie is the means by which one
  110. * can get to OBP and PCI controller specific information
  111. * for a PCI device.
  112. */
  113. static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
  114. struct pci_dev *pdev,
  115. int bus_prom_node)
  116. {
  117. struct linux_prom_pci_registers pregs[PROMREG_MAX];
  118. struct pcidev_cookie *pcp;
  119. int device_prom_node, nregs, err;
  120. device_prom_node = find_device_prom_node(pbm, pdev, bus_prom_node,
  121. pregs, &nregs);
  122. if (device_prom_node == 0) {
  123. /* If it is not in the OBP device tree then
  124. * there must be a damn good reason for it.
  125. *
  126. * So what we do is delete the device from the
  127. * PCI device tree completely. This scenario
  128. * is seen, for example, on CP1500 for the
  129. * second EBUS/HappyMeal pair if the external
  130. * connector for it is not present.
  131. */
  132. pci_remove_bus_device(pdev);
  133. return;
  134. }
  135. pcp = kmalloc(sizeof(*pcp), GFP_ATOMIC);
  136. if (pcp == NULL) {
  137. prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
  138. prom_halt();
  139. }
  140. pcp->pbm = pbm;
  141. pcp->prom_node = device_prom_node;
  142. memcpy(pcp->prom_regs, pregs, sizeof(pcp->prom_regs));
  143. pcp->num_prom_regs = nregs;
  144. err = prom_getproperty(device_prom_node, "name",
  145. pcp->prom_name, sizeof(pcp->prom_name));
  146. if (err > 0)
  147. pcp->prom_name[err] = 0;
  148. else
  149. pcp->prom_name[0] = 0;
  150. err = prom_getproperty(device_prom_node,
  151. "assigned-addresses",
  152. (char *)pcp->prom_assignments,
  153. sizeof(pcp->prom_assignments));
  154. if (err == 0 || err == -1)
  155. pcp->num_prom_assignments = 0;
  156. else
  157. pcp->num_prom_assignments =
  158. (err / sizeof(pcp->prom_assignments[0]));
  159. if (strcmp(pcp->prom_name, "ebus") == 0) {
  160. struct linux_prom_ebus_ranges erng[PROM_PCIRNG_MAX];
  161. int iter;
  162. /* EBUS is special... */
  163. err = prom_getproperty(device_prom_node, "ranges",
  164. (char *)&erng[0], sizeof(erng));
  165. if (err == 0 || err == -1) {
  166. prom_printf("EBUS: Fatal error, no range property\n");
  167. prom_halt();
  168. }
  169. err = (err / sizeof(erng[0]));
  170. for(iter = 0; iter < err; iter++) {
  171. struct linux_prom_ebus_ranges *ep = &erng[iter];
  172. struct linux_prom_pci_registers *ap;
  173. ap = &pcp->prom_assignments[iter];
  174. ap->phys_hi = ep->parent_phys_hi;
  175. ap->phys_mid = ep->parent_phys_mid;
  176. ap->phys_lo = ep->parent_phys_lo;
  177. ap->size_hi = 0;
  178. ap->size_lo = ep->size;
  179. }
  180. pcp->num_prom_assignments = err;
  181. }
  182. fixup_obp_assignments(pdev, pcp);
  183. pdev->sysdata = pcp;
  184. }
  185. void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
  186. struct pci_pbm_info *pbm,
  187. int prom_node)
  188. {
  189. struct pci_dev *pdev, *pdev_next;
  190. struct pci_bus *this_pbus, *pbus_next;
  191. /* This must be _safe because the cookie fillin
  192. routine can delete devices from the tree. */
  193. list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
  194. pdev_cookie_fillin(pbm, pdev, prom_node);
  195. list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
  196. struct pcidev_cookie *pcp = this_pbus->self->sysdata;
  197. pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
  198. }
  199. }
  200. static void __init bad_assignment(struct pci_dev *pdev,
  201. struct linux_prom_pci_registers *ap,
  202. struct resource *res,
  203. int do_prom_halt)
  204. {
  205. prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
  206. pdev->bus->number, pdev->devfn);
  207. if (ap)
  208. prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
  209. ap->phys_hi, ap->phys_mid, ap->phys_lo,
  210. ap->size_hi, ap->size_lo);
  211. if (res)
  212. prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
  213. res->start, res->end, res->flags);
  214. prom_printf("Please email this information to davem@redhat.com\n");
  215. if (do_prom_halt)
  216. prom_halt();
  217. }
  218. static struct resource *
  219. __init get_root_resource(struct linux_prom_pci_registers *ap,
  220. struct pci_pbm_info *pbm)
  221. {
  222. int space = (ap->phys_hi >> 24) & 3;
  223. switch (space) {
  224. case 0:
  225. /* Configuration space, silently ignore it. */
  226. return NULL;
  227. case 1:
  228. /* 16-bit IO space */
  229. return &pbm->io_space;
  230. case 2:
  231. /* 32-bit MEM space */
  232. return &pbm->mem_space;
  233. case 3:
  234. /* 64-bit MEM space, these are allocated out of
  235. * the 32-bit mem_space range for the PBM, ie.
  236. * we just zero out the upper 32-bits.
  237. */
  238. return &pbm->mem_space;
  239. default:
  240. printk("PCI: What is resource space %x? "
  241. "Tell davem@redhat.com about it!\n", space);
  242. return NULL;
  243. };
  244. }
  245. static struct resource *
  246. __init get_device_resource(struct linux_prom_pci_registers *ap,
  247. struct pci_dev *pdev)
  248. {
  249. struct resource *res;
  250. int breg = (ap->phys_hi & 0xff);
  251. switch (breg) {
  252. case PCI_ROM_ADDRESS:
  253. /* Unfortunately I have seen several cases where
  254. * buggy FCODE uses a space value of '1' (I/O space)
  255. * in the register property for the ROM address
  256. * so disable this sanity check for now.
  257. */
  258. #if 0
  259. {
  260. int space = (ap->phys_hi >> 24) & 3;
  261. /* It had better be MEM space. */
  262. if (space != 2)
  263. bad_assignment(pdev, ap, NULL, 0);
  264. }
  265. #endif
  266. res = &pdev->resource[PCI_ROM_RESOURCE];
  267. break;
  268. case PCI_BASE_ADDRESS_0:
  269. case PCI_BASE_ADDRESS_1:
  270. case PCI_BASE_ADDRESS_2:
  271. case PCI_BASE_ADDRESS_3:
  272. case PCI_BASE_ADDRESS_4:
  273. case PCI_BASE_ADDRESS_5:
  274. res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
  275. break;
  276. default:
  277. bad_assignment(pdev, ap, NULL, 0);
  278. res = NULL;
  279. break;
  280. };
  281. return res;
  282. }
  283. static int __init pdev_resource_collisions_expected(struct pci_dev *pdev)
  284. {
  285. if (pdev->vendor != PCI_VENDOR_ID_SUN)
  286. return 0;
  287. if (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS ||
  288. pdev->device == PCI_DEVICE_ID_SUN_RIO_1394 ||
  289. pdev->device == PCI_DEVICE_ID_SUN_RIO_USB)
  290. return 1;
  291. return 0;
  292. }
  293. static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
  294. struct pci_dev *pdev)
  295. {
  296. struct pcidev_cookie *pcp = pdev->sysdata;
  297. int i;
  298. for (i = 0; i < pcp->num_prom_assignments; i++) {
  299. struct linux_prom_pci_registers *ap;
  300. struct resource *root, *res;
  301. /* The format of this property is specified in
  302. * the PCI Bus Binding to IEEE1275-1994.
  303. */
  304. ap = &pcp->prom_assignments[i];
  305. root = get_root_resource(ap, pbm);
  306. res = get_device_resource(ap, pdev);
  307. if (root == NULL || res == NULL ||
  308. res->flags == 0)
  309. continue;
  310. /* Ok we know which resource this PROM assignment is
  311. * for, sanity check it.
  312. */
  313. if ((res->start & 0xffffffffUL) != ap->phys_lo)
  314. bad_assignment(pdev, ap, res, 1);
  315. /* If it is a 64-bit MEM space assignment, verify that
  316. * the resource is too and that the upper 32-bits match.
  317. */
  318. if (((ap->phys_hi >> 24) & 3) == 3) {
  319. if (((res->flags & IORESOURCE_MEM) == 0) ||
  320. ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  321. != PCI_BASE_ADDRESS_MEM_TYPE_64))
  322. bad_assignment(pdev, ap, res, 1);
  323. if ((res->start >> 32) != ap->phys_mid)
  324. bad_assignment(pdev, ap, res, 1);
  325. /* PBM cannot generate cpu initiated PIOs
  326. * to the full 64-bit space. Therefore the
  327. * upper 32-bits better be zero. If it is
  328. * not, just skip it and we will assign it
  329. * properly ourselves.
  330. */
  331. if ((res->start >> 32) != 0UL) {
  332. printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
  333. "%016lx for region %ld on device %s\n",
  334. res->start, (res - &pdev->resource[0]), pci_name(pdev));
  335. continue;
  336. }
  337. }
  338. /* Adjust the resource into the physical address space
  339. * of this PBM.
  340. */
  341. pbm->parent->resource_adjust(pdev, res, root);
  342. if (request_resource(root, res) < 0) {
  343. /* OK, there is some conflict. But this is fine
  344. * since we'll reassign it in the fixup pass.
  345. *
  346. * We notify the user that OBP made an error if it
  347. * is a case we don't expect.
  348. */
  349. if (!pdev_resource_collisions_expected(pdev)) {
  350. printk(KERN_ERR "PCI: Address space collision on region %ld "
  351. "[%016lx:%016lx] of device %s\n",
  352. (res - &pdev->resource[0]),
  353. res->start, res->end,
  354. pci_name(pdev));
  355. }
  356. }
  357. }
  358. }
  359. void __init pci_record_assignments(struct pci_pbm_info *pbm,
  360. struct pci_bus *pbus)
  361. {
  362. struct pci_dev *dev;
  363. struct pci_bus *bus;
  364. list_for_each_entry(dev, &pbus->devices, bus_list)
  365. pdev_record_assignments(pbm, dev);
  366. list_for_each_entry(bus, &pbus->children, node)
  367. pci_record_assignments(pbm, bus);
  368. }
  369. /* Return non-zero if PDEV has implicit I/O resources even
  370. * though it may not have an I/O base address register
  371. * active.
  372. */
  373. static int __init has_implicit_io(struct pci_dev *pdev)
  374. {
  375. int class = pdev->class >> 8;
  376. if (class == PCI_CLASS_NOT_DEFINED ||
  377. class == PCI_CLASS_NOT_DEFINED_VGA ||
  378. class == PCI_CLASS_STORAGE_IDE ||
  379. (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  380. return 1;
  381. return 0;
  382. }
  383. static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
  384. struct pci_dev *pdev)
  385. {
  386. u32 reg;
  387. u16 cmd;
  388. int i, io_seen, mem_seen;
  389. io_seen = mem_seen = 0;
  390. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  391. struct resource *root, *res;
  392. unsigned long size, min, max, align;
  393. res = &pdev->resource[i];
  394. if (res->flags & IORESOURCE_IO)
  395. io_seen++;
  396. else if (res->flags & IORESOURCE_MEM)
  397. mem_seen++;
  398. /* If it is already assigned or the resource does
  399. * not exist, there is nothing to do.
  400. */
  401. if (res->parent != NULL || res->flags == 0UL)
  402. continue;
  403. /* Determine the root we allocate from. */
  404. if (res->flags & IORESOURCE_IO) {
  405. root = &pbm->io_space;
  406. min = root->start + 0x400UL;
  407. max = root->end;
  408. } else {
  409. root = &pbm->mem_space;
  410. min = root->start;
  411. max = min + 0x80000000UL;
  412. }
  413. size = res->end - res->start;
  414. align = size + 1;
  415. if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
  416. /* uh oh */
  417. prom_printf("PCI: Failed to allocate resource %d for %s\n",
  418. i, pci_name(pdev));
  419. prom_halt();
  420. }
  421. /* Update PCI config space. */
  422. pbm->parent->base_address_update(pdev, i);
  423. }
  424. /* Special case, disable the ROM. Several devices
  425. * act funny (ie. do not respond to memory space writes)
  426. * when it is left enabled. A good example are Qlogic,ISP
  427. * adapters.
  428. */
  429. pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
  430. reg &= ~PCI_ROM_ADDRESS_ENABLE;
  431. pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
  432. /* If we saw I/O or MEM resources, enable appropriate
  433. * bits in PCI command register.
  434. */
  435. if (io_seen || mem_seen) {
  436. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  437. if (io_seen || has_implicit_io(pdev))
  438. cmd |= PCI_COMMAND_IO;
  439. if (mem_seen)
  440. cmd |= PCI_COMMAND_MEMORY;
  441. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  442. }
  443. /* If this is a PCI bridge or an IDE controller,
  444. * enable bus mastering. In the former case also
  445. * set the cache line size correctly.
  446. */
  447. if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
  448. (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
  449. ((pdev->class & 0x80) != 0))) {
  450. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  451. cmd |= PCI_COMMAND_MASTER;
  452. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  453. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
  454. pci_write_config_byte(pdev,
  455. PCI_CACHE_LINE_SIZE,
  456. (64 / sizeof(u32)));
  457. }
  458. }
  459. void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
  460. struct pci_bus *pbus)
  461. {
  462. struct pci_dev *dev;
  463. struct pci_bus *bus;
  464. list_for_each_entry(dev, &pbus->devices, bus_list)
  465. pdev_assign_unassigned(pbm, dev);
  466. list_for_each_entry(bus, &pbus->children, node)
  467. pci_assign_unassigned(pbm, bus);
  468. }
  469. static inline unsigned int pci_slot_swivel(struct pci_pbm_info *pbm,
  470. struct pci_dev *toplevel_pdev,
  471. struct pci_dev *pdev,
  472. unsigned int interrupt)
  473. {
  474. unsigned int ret;
  475. if (unlikely(interrupt < 1 || interrupt > 4)) {
  476. printk("%s: Device %s interrupt value of %u is strange.\n",
  477. pbm->name, pci_name(pdev), interrupt);
  478. return interrupt;
  479. }
  480. ret = ((interrupt - 1 + (PCI_SLOT(pdev->devfn) & 3)) & 3) + 1;
  481. printk("%s: %s IRQ Swivel %s [%x:%x] -> [%x]\n",
  482. pbm->name, pci_name(toplevel_pdev), pci_name(pdev),
  483. interrupt, PCI_SLOT(pdev->devfn), ret);
  484. return ret;
  485. }
  486. static inline unsigned int pci_apply_intmap(struct pci_pbm_info *pbm,
  487. struct pci_dev *toplevel_pdev,
  488. struct pci_dev *pbus,
  489. struct pci_dev *pdev,
  490. unsigned int interrupt,
  491. unsigned int *cnode)
  492. {
  493. struct linux_prom_pci_intmap imap[PROM_PCIIMAP_MAX];
  494. struct linux_prom_pci_intmask imask;
  495. struct pcidev_cookie *pbus_pcp = pbus->sysdata;
  496. struct pcidev_cookie *pdev_pcp = pdev->sysdata;
  497. struct linux_prom_pci_registers *pregs = pdev_pcp->prom_regs;
  498. int plen, num_imap, i;
  499. unsigned int hi, mid, lo, irq, orig_interrupt;
  500. *cnode = pbus_pcp->prom_node;
  501. plen = prom_getproperty(pbus_pcp->prom_node, "interrupt-map",
  502. (char *) &imap[0], sizeof(imap));
  503. if (plen <= 0 ||
  504. (plen % sizeof(struct linux_prom_pci_intmap)) != 0) {
  505. printk("%s: Device %s interrupt-map has bad len %d\n",
  506. pbm->name, pci_name(pbus), plen);
  507. goto no_intmap;
  508. }
  509. num_imap = plen / sizeof(struct linux_prom_pci_intmap);
  510. plen = prom_getproperty(pbus_pcp->prom_node, "interrupt-map-mask",
  511. (char *) &imask, sizeof(imask));
  512. if (plen <= 0 ||
  513. (plen % sizeof(struct linux_prom_pci_intmask)) != 0) {
  514. printk("%s: Device %s interrupt-map-mask has bad len %d\n",
  515. pbm->name, pci_name(pbus), plen);
  516. goto no_intmap;
  517. }
  518. orig_interrupt = interrupt;
  519. hi = pregs->phys_hi & imask.phys_hi;
  520. mid = pregs->phys_mid & imask.phys_mid;
  521. lo = pregs->phys_lo & imask.phys_lo;
  522. irq = interrupt & imask.interrupt;
  523. for (i = 0; i < num_imap; i++) {
  524. if (imap[i].phys_hi == hi &&
  525. imap[i].phys_mid == mid &&
  526. imap[i].phys_lo == lo &&
  527. imap[i].interrupt == irq) {
  528. *cnode = imap[i].cnode;
  529. interrupt = imap[i].cinterrupt;
  530. }
  531. }
  532. printk("%s: %s MAP BUS %s DEV %s [%x] -> [%x]\n",
  533. pbm->name, pci_name(toplevel_pdev),
  534. pci_name(pbus), pci_name(pdev),
  535. orig_interrupt, interrupt);
  536. no_intmap:
  537. return interrupt;
  538. }
  539. /* For each PCI bus on the way to the root:
  540. * 1) If it has an interrupt-map property, apply it.
  541. * 2) Else, swivel the interrupt number based upon the PCI device number.
  542. *
  543. * Return the "IRQ controller" node. If this is the PBM's device node,
  544. * all interrupt translations are complete, else we should use that node's
  545. * "reg" property to apply the PBM's "interrupt-{map,mask}" to the interrupt.
  546. */
  547. static unsigned int __init pci_intmap_match_to_root(struct pci_pbm_info *pbm,
  548. struct pci_dev *pdev,
  549. unsigned int *interrupt)
  550. {
  551. struct pci_dev *toplevel_pdev = pdev;
  552. struct pcidev_cookie *toplevel_pcp = toplevel_pdev->sysdata;
  553. unsigned int cnode = toplevel_pcp->prom_node;
  554. while (pdev->bus->number != pbm->pci_first_busno) {
  555. struct pci_dev *pbus = pdev->bus->self;
  556. struct pcidev_cookie *pcp = pbus->sysdata;
  557. int plen;
  558. plen = prom_getproplen(pcp->prom_node, "interrupt-map");
  559. if (plen <= 0) {
  560. *interrupt = pci_slot_swivel(pbm, toplevel_pdev,
  561. pdev, *interrupt);
  562. cnode = pcp->prom_node;
  563. } else {
  564. *interrupt = pci_apply_intmap(pbm, toplevel_pdev,
  565. pbus, pdev,
  566. *interrupt, &cnode);
  567. while (pcp->prom_node != cnode &&
  568. pbus->bus->number != pbm->pci_first_busno) {
  569. pbus = pbus->bus->self;
  570. pcp = pbus->sysdata;
  571. }
  572. }
  573. pdev = pbus;
  574. if (cnode == pbm->prom_node)
  575. break;
  576. }
  577. return cnode;
  578. }
  579. static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
  580. {
  581. struct pcidev_cookie *dev_pcp = pdev->sysdata;
  582. struct pci_pbm_info *pbm = dev_pcp->pbm;
  583. struct linux_prom_pci_registers reg[PROMREG_MAX];
  584. unsigned int hi, mid, lo, irq;
  585. int i, cnode, plen;
  586. cnode = pci_intmap_match_to_root(pbm, pdev, interrupt);
  587. if (cnode == pbm->prom_node)
  588. goto success;
  589. plen = prom_getproperty(cnode, "reg", (char *) reg, sizeof(reg));
  590. if (plen <= 0 ||
  591. (plen % sizeof(struct linux_prom_pci_registers)) != 0) {
  592. printk("%s: OBP node %x reg property has bad len %d\n",
  593. pbm->name, cnode, plen);
  594. goto fail;
  595. }
  596. hi = reg[0].phys_hi & pbm->pbm_intmask.phys_hi;
  597. mid = reg[0].phys_mid & pbm->pbm_intmask.phys_mid;
  598. lo = reg[0].phys_lo & pbm->pbm_intmask.phys_lo;
  599. irq = *interrupt & pbm->pbm_intmask.interrupt;
  600. for (i = 0; i < pbm->num_pbm_intmap; i++) {
  601. struct linux_prom_pci_intmap *intmap;
  602. intmap = &pbm->pbm_intmap[i];
  603. if (intmap->phys_hi == hi &&
  604. intmap->phys_mid == mid &&
  605. intmap->phys_lo == lo &&
  606. intmap->interrupt == irq) {
  607. *interrupt = intmap->cinterrupt;
  608. goto success;
  609. }
  610. }
  611. fail:
  612. return 0;
  613. success:
  614. printk("PCI-IRQ: Routing bus[%2x] slot[%2x] to INO[%02x]\n",
  615. pdev->bus->number, PCI_SLOT(pdev->devfn),
  616. *interrupt);
  617. return 1;
  618. }
  619. static void __init pdev_fixup_irq(struct pci_dev *pdev)
  620. {
  621. struct pcidev_cookie *pcp = pdev->sysdata;
  622. struct pci_pbm_info *pbm = pcp->pbm;
  623. struct pci_controller_info *p = pbm->parent;
  624. unsigned int portid = pbm->portid;
  625. unsigned int prom_irq;
  626. int prom_node = pcp->prom_node;
  627. int err;
  628. /* If this is an empty EBUS device, sometimes OBP fails to
  629. * give it a valid fully specified interrupts property.
  630. * The EBUS hooked up to SunHME on PCI I/O boards of
  631. * Ex000 systems is one such case.
  632. *
  633. * The interrupt is not important so just ignore it.
  634. */
  635. if (pdev->vendor == PCI_VENDOR_ID_SUN &&
  636. pdev->device == PCI_DEVICE_ID_SUN_EBUS &&
  637. !prom_getchild(prom_node)) {
  638. pdev->irq = 0;
  639. return;
  640. }
  641. err = prom_getproperty(prom_node, "interrupts",
  642. (char *)&prom_irq, sizeof(prom_irq));
  643. if (err == 0 || err == -1) {
  644. pdev->irq = 0;
  645. return;
  646. }
  647. if (tlb_type != hypervisor) {
  648. /* Fully specified already? */
  649. if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
  650. pdev->irq = p->irq_build(pbm, pdev, prom_irq);
  651. goto have_irq;
  652. }
  653. /* An onboard device? (bit 5 set) */
  654. if ((prom_irq & PCI_IRQ_INO) & 0x20) {
  655. pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
  656. goto have_irq;
  657. }
  658. }
  659. /* Can we find a matching entry in the interrupt-map? */
  660. if (pci_intmap_match(pdev, &prom_irq)) {
  661. pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
  662. goto have_irq;
  663. }
  664. /* Ok, we have to do it the hard way. */
  665. {
  666. unsigned int bus, slot, line;
  667. bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
  668. /* If we have a legal interrupt property, use it as
  669. * the IRQ line.
  670. */
  671. if (prom_irq > 0 && prom_irq < 5) {
  672. line = ((prom_irq - 1) & 3);
  673. } else {
  674. u8 pci_irq_line;
  675. /* Else just directly consult PCI config space. */
  676. pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
  677. line = ((pci_irq_line - 1) & 3);
  678. }
  679. /* Now figure out the slot.
  680. *
  681. * Basically, device number zero on the top-level bus is
  682. * always the PCI host controller. Slot 0 is then device 1.
  683. * PBM A supports two external slots (0 and 1), and PBM B
  684. * supports 4 external slots (0, 1, 2, and 3). On-board PCI
  685. * devices are wired to device numbers outside of these
  686. * ranges. -DaveM
  687. */
  688. if (pdev->bus->number == pbm->pci_first_busno) {
  689. slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
  690. } else {
  691. struct pci_dev *bus_dev;
  692. /* Underneath a bridge, use slot number of parent
  693. * bridge which is closest to the PBM.
  694. */
  695. bus_dev = pdev->bus->self;
  696. while (bus_dev->bus &&
  697. bus_dev->bus->number != pbm->pci_first_busno)
  698. bus_dev = bus_dev->bus->self;
  699. slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
  700. }
  701. slot = slot << 2;
  702. pdev->irq = p->irq_build(pbm, pdev,
  703. ((portid << 6) & PCI_IRQ_IGN) |
  704. (bus | slot | line));
  705. }
  706. have_irq:
  707. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
  708. pdev->irq & PCI_IRQ_INO);
  709. }
  710. void __init pci_fixup_irq(struct pci_pbm_info *pbm,
  711. struct pci_bus *pbus)
  712. {
  713. struct pci_dev *dev;
  714. struct pci_bus *bus;
  715. list_for_each_entry(dev, &pbus->devices, bus_list)
  716. pdev_fixup_irq(dev);
  717. list_for_each_entry(bus, &pbus->children, node)
  718. pci_fixup_irq(pbm, bus);
  719. }
  720. static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
  721. {
  722. u16 cmd;
  723. u8 hdr_type, min_gnt, ltimer;
  724. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  725. cmd |= PCI_COMMAND_MASTER;
  726. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  727. /* Read it back, if the mastering bit did not
  728. * get set, the device does not support bus
  729. * mastering so we have nothing to do here.
  730. */
  731. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  732. if ((cmd & PCI_COMMAND_MASTER) == 0)
  733. return;
  734. /* Set correct cache line size, 64-byte on all
  735. * Sparc64 PCI systems. Note that the value is
  736. * measured in 32-bit words.
  737. */
  738. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  739. 64 / sizeof(u32));
  740. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
  741. hdr_type &= ~0x80;
  742. if (hdr_type != PCI_HEADER_TYPE_NORMAL)
  743. return;
  744. /* If the latency timer is already programmed with a non-zero
  745. * value, assume whoever set it (OBP or whoever) knows what
  746. * they are doing.
  747. */
  748. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
  749. if (ltimer != 0)
  750. return;
  751. /* XXX Since I'm tipping off the min grant value to
  752. * XXX choose a suitable latency timer value, I also
  753. * XXX considered making use of the max latency value
  754. * XXX as well. Unfortunately I've seen too many bogusly
  755. * XXX low settings for it to the point where it lacks
  756. * XXX any usefulness. In one case, an ethernet card
  757. * XXX claimed a min grant of 10 and a max latency of 5.
  758. * XXX Now, if I had two such cards on the same bus I
  759. * XXX could not set the desired burst period (calculated
  760. * XXX from min grant) without violating the max latency
  761. * XXX bound. Duh...
  762. * XXX
  763. * XXX I blame dumb PC bios implementors for stuff like
  764. * XXX this, most of them don't even try to do something
  765. * XXX sensible with latency timer values and just set some
  766. * XXX default value (usually 32) into every device.
  767. */
  768. pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
  769. if (min_gnt == 0) {
  770. /* If no min_gnt setting then use a default
  771. * value.
  772. */
  773. if (is_66mhz)
  774. ltimer = 16;
  775. else
  776. ltimer = 32;
  777. } else {
  778. int shift_factor;
  779. if (is_66mhz)
  780. shift_factor = 2;
  781. else
  782. shift_factor = 3;
  783. /* Use a default value when the min_gnt value
  784. * is erroneously high.
  785. */
  786. if (((unsigned int) min_gnt << shift_factor) > 512 ||
  787. ((min_gnt << shift_factor) & 0xff) == 0) {
  788. ltimer = 8 << shift_factor;
  789. } else {
  790. ltimer = min_gnt << shift_factor;
  791. }
  792. }
  793. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
  794. }
  795. void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
  796. struct pci_bus *pbus)
  797. {
  798. struct pci_dev *pdev;
  799. int all_are_66mhz;
  800. u16 status;
  801. if (pbm->is_66mhz_capable == 0) {
  802. all_are_66mhz = 0;
  803. goto out;
  804. }
  805. all_are_66mhz = 1;
  806. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  807. pci_read_config_word(pdev, PCI_STATUS, &status);
  808. if (!(status & PCI_STATUS_66MHZ)) {
  809. all_are_66mhz = 0;
  810. break;
  811. }
  812. }
  813. out:
  814. pbm->all_devs_66mhz = all_are_66mhz;
  815. printk("PCI%d(PBM%c): Bus running at %dMHz\n",
  816. pbm->parent->index,
  817. (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
  818. (all_are_66mhz ? 66 : 33));
  819. }
  820. void pci_setup_busmastering(struct pci_pbm_info *pbm,
  821. struct pci_bus *pbus)
  822. {
  823. struct pci_dev *dev;
  824. struct pci_bus *bus;
  825. int is_66mhz;
  826. is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
  827. list_for_each_entry(dev, &pbus->devices, bus_list)
  828. pdev_setup_busmastering(dev, is_66mhz);
  829. list_for_each_entry(bus, &pbus->children, node)
  830. pci_setup_busmastering(pbm, bus);
  831. }
  832. void pci_register_legacy_regions(struct resource *io_res,
  833. struct resource *mem_res)
  834. {
  835. struct resource *p;
  836. /* VGA Video RAM. */
  837. p = kzalloc(sizeof(*p), GFP_KERNEL);
  838. if (!p)
  839. return;
  840. p->name = "Video RAM area";
  841. p->start = mem_res->start + 0xa0000UL;
  842. p->end = p->start + 0x1ffffUL;
  843. p->flags = IORESOURCE_BUSY;
  844. request_resource(mem_res, p);
  845. p = kzalloc(sizeof(*p), GFP_KERNEL);
  846. if (!p)
  847. return;
  848. p->name = "System ROM";
  849. p->start = mem_res->start + 0xf0000UL;
  850. p->end = p->start + 0xffffUL;
  851. p->flags = IORESOURCE_BUSY;
  852. request_resource(mem_res, p);
  853. p = kzalloc(sizeof(*p), GFP_KERNEL);
  854. if (!p)
  855. return;
  856. p->name = "Video ROM";
  857. p->start = mem_res->start + 0xc0000UL;
  858. p->end = p->start + 0x7fffUL;
  859. p->flags = IORESOURCE_BUSY;
  860. request_resource(mem_res, p);
  861. }
  862. /* Generic helper routines for PCI error reporting. */
  863. void pci_scan_for_target_abort(struct pci_controller_info *p,
  864. struct pci_pbm_info *pbm,
  865. struct pci_bus *pbus)
  866. {
  867. struct pci_dev *pdev;
  868. struct pci_bus *bus;
  869. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  870. u16 status, error_bits;
  871. pci_read_config_word(pdev, PCI_STATUS, &status);
  872. error_bits =
  873. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  874. PCI_STATUS_REC_TARGET_ABORT));
  875. if (error_bits) {
  876. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  877. printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
  878. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  879. pci_name(pdev), status);
  880. }
  881. }
  882. list_for_each_entry(bus, &pbus->children, node)
  883. pci_scan_for_target_abort(p, pbm, bus);
  884. }
  885. void pci_scan_for_master_abort(struct pci_controller_info *p,
  886. struct pci_pbm_info *pbm,
  887. struct pci_bus *pbus)
  888. {
  889. struct pci_dev *pdev;
  890. struct pci_bus *bus;
  891. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  892. u16 status, error_bits;
  893. pci_read_config_word(pdev, PCI_STATUS, &status);
  894. error_bits =
  895. (status & (PCI_STATUS_REC_MASTER_ABORT));
  896. if (error_bits) {
  897. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  898. printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
  899. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  900. pci_name(pdev), status);
  901. }
  902. }
  903. list_for_each_entry(bus, &pbus->children, node)
  904. pci_scan_for_master_abort(p, pbm, bus);
  905. }
  906. void pci_scan_for_parity_error(struct pci_controller_info *p,
  907. struct pci_pbm_info *pbm,
  908. struct pci_bus *pbus)
  909. {
  910. struct pci_dev *pdev;
  911. struct pci_bus *bus;
  912. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  913. u16 status, error_bits;
  914. pci_read_config_word(pdev, PCI_STATUS, &status);
  915. error_bits =
  916. (status & (PCI_STATUS_PARITY |
  917. PCI_STATUS_DETECTED_PARITY));
  918. if (error_bits) {
  919. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  920. printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
  921. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  922. pci_name(pdev), status);
  923. }
  924. }
  925. list_for_each_entry(bus, &pbus->children, node)
  926. pci_scan_for_parity_error(p, pbm, bus);
  927. }