pci_common.c 29 KB

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