pci_common.c 28 KB

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