pci_common.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. /* Fully specified already? */
  615. if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
  616. pdev->irq = p->irq_build(pbm, pdev, prom_irq);
  617. goto have_irq;
  618. }
  619. /* An onboard device? (bit 5 set) */
  620. if ((prom_irq & PCI_IRQ_INO) & 0x20) {
  621. pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
  622. goto have_irq;
  623. }
  624. /* Can we find a matching entry in the interrupt-map? */
  625. if (pci_intmap_match(pdev, &prom_irq)) {
  626. pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
  627. goto have_irq;
  628. }
  629. /* Ok, we have to do it the hard way. */
  630. {
  631. unsigned int bus, slot, line;
  632. bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
  633. /* If we have a legal interrupt property, use it as
  634. * the IRQ line.
  635. */
  636. if (prom_irq > 0 && prom_irq < 5) {
  637. line = ((prom_irq - 1) & 3);
  638. } else {
  639. u8 pci_irq_line;
  640. /* Else just directly consult PCI config space. */
  641. pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
  642. line = ((pci_irq_line - 1) & 3);
  643. }
  644. /* Now figure out the slot.
  645. *
  646. * Basically, device number zero on the top-level bus is
  647. * always the PCI host controller. Slot 0 is then device 1.
  648. * PBM A supports two external slots (0 and 1), and PBM B
  649. * supports 4 external slots (0, 1, 2, and 3). On-board PCI
  650. * devices are wired to device numbers outside of these
  651. * ranges. -DaveM
  652. */
  653. if (pdev->bus->number == pbm->pci_first_busno) {
  654. slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
  655. } else {
  656. struct pci_dev *bus_dev;
  657. /* Underneath a bridge, use slot number of parent
  658. * bridge which is closest to the PBM.
  659. */
  660. bus_dev = pdev->bus->self;
  661. while (bus_dev->bus &&
  662. bus_dev->bus->number != pbm->pci_first_busno)
  663. bus_dev = bus_dev->bus->self;
  664. slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
  665. }
  666. slot = slot << 2;
  667. pdev->irq = p->irq_build(pbm, pdev,
  668. ((portid << 6) & PCI_IRQ_IGN) |
  669. (bus | slot | line));
  670. }
  671. have_irq:
  672. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
  673. pdev->irq & PCI_IRQ_INO);
  674. }
  675. void __init pci_fixup_irq(struct pci_pbm_info *pbm,
  676. struct pci_bus *pbus)
  677. {
  678. struct pci_dev *dev;
  679. struct pci_bus *bus;
  680. list_for_each_entry(dev, &pbus->devices, bus_list)
  681. pdev_fixup_irq(dev);
  682. list_for_each_entry(bus, &pbus->children, node)
  683. pci_fixup_irq(pbm, bus);
  684. }
  685. static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
  686. {
  687. u16 cmd;
  688. u8 hdr_type, min_gnt, ltimer;
  689. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  690. cmd |= PCI_COMMAND_MASTER;
  691. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  692. /* Read it back, if the mastering bit did not
  693. * get set, the device does not support bus
  694. * mastering so we have nothing to do here.
  695. */
  696. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  697. if ((cmd & PCI_COMMAND_MASTER) == 0)
  698. return;
  699. /* Set correct cache line size, 64-byte on all
  700. * Sparc64 PCI systems. Note that the value is
  701. * measured in 32-bit words.
  702. */
  703. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  704. 64 / sizeof(u32));
  705. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
  706. hdr_type &= ~0x80;
  707. if (hdr_type != PCI_HEADER_TYPE_NORMAL)
  708. return;
  709. /* If the latency timer is already programmed with a non-zero
  710. * value, assume whoever set it (OBP or whoever) knows what
  711. * they are doing.
  712. */
  713. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
  714. if (ltimer != 0)
  715. return;
  716. /* XXX Since I'm tipping off the min grant value to
  717. * XXX choose a suitable latency timer value, I also
  718. * XXX considered making use of the max latency value
  719. * XXX as well. Unfortunately I've seen too many bogusly
  720. * XXX low settings for it to the point where it lacks
  721. * XXX any usefulness. In one case, an ethernet card
  722. * XXX claimed a min grant of 10 and a max latency of 5.
  723. * XXX Now, if I had two such cards on the same bus I
  724. * XXX could not set the desired burst period (calculated
  725. * XXX from min grant) without violating the max latency
  726. * XXX bound. Duh...
  727. * XXX
  728. * XXX I blame dumb PC bios implementors for stuff like
  729. * XXX this, most of them don't even try to do something
  730. * XXX sensible with latency timer values and just set some
  731. * XXX default value (usually 32) into every device.
  732. */
  733. pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
  734. if (min_gnt == 0) {
  735. /* If no min_gnt setting then use a default
  736. * value.
  737. */
  738. if (is_66mhz)
  739. ltimer = 16;
  740. else
  741. ltimer = 32;
  742. } else {
  743. int shift_factor;
  744. if (is_66mhz)
  745. shift_factor = 2;
  746. else
  747. shift_factor = 3;
  748. /* Use a default value when the min_gnt value
  749. * is erroneously high.
  750. */
  751. if (((unsigned int) min_gnt << shift_factor) > 512 ||
  752. ((min_gnt << shift_factor) & 0xff) == 0) {
  753. ltimer = 8 << shift_factor;
  754. } else {
  755. ltimer = min_gnt << shift_factor;
  756. }
  757. }
  758. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
  759. }
  760. void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
  761. struct pci_bus *pbus)
  762. {
  763. struct pci_dev *pdev;
  764. int all_are_66mhz;
  765. u16 status;
  766. if (pbm->is_66mhz_capable == 0) {
  767. all_are_66mhz = 0;
  768. goto out;
  769. }
  770. all_are_66mhz = 1;
  771. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  772. pci_read_config_word(pdev, PCI_STATUS, &status);
  773. if (!(status & PCI_STATUS_66MHZ)) {
  774. all_are_66mhz = 0;
  775. break;
  776. }
  777. }
  778. out:
  779. pbm->all_devs_66mhz = all_are_66mhz;
  780. printk("PCI%d(PBM%c): Bus running at %dMHz\n",
  781. pbm->parent->index,
  782. (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
  783. (all_are_66mhz ? 66 : 33));
  784. }
  785. void pci_setup_busmastering(struct pci_pbm_info *pbm,
  786. struct pci_bus *pbus)
  787. {
  788. struct pci_dev *dev;
  789. struct pci_bus *bus;
  790. int is_66mhz;
  791. is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
  792. list_for_each_entry(dev, &pbus->devices, bus_list)
  793. pdev_setup_busmastering(dev, is_66mhz);
  794. list_for_each_entry(bus, &pbus->children, node)
  795. pci_setup_busmastering(pbm, bus);
  796. }
  797. void pci_register_legacy_regions(struct resource *io_res,
  798. struct resource *mem_res)
  799. {
  800. struct resource *p;
  801. /* VGA Video RAM. */
  802. p = kmalloc(sizeof(*p), GFP_KERNEL);
  803. if (!p)
  804. return;
  805. memset(p, 0, sizeof(*p));
  806. p->name = "Video RAM area";
  807. p->start = mem_res->start + 0xa0000UL;
  808. p->end = p->start + 0x1ffffUL;
  809. p->flags = IORESOURCE_BUSY;
  810. request_resource(mem_res, p);
  811. p = kmalloc(sizeof(*p), GFP_KERNEL);
  812. if (!p)
  813. return;
  814. memset(p, 0, sizeof(*p));
  815. p->name = "System ROM";
  816. p->start = mem_res->start + 0xf0000UL;
  817. p->end = p->start + 0xffffUL;
  818. p->flags = IORESOURCE_BUSY;
  819. request_resource(mem_res, p);
  820. p = kmalloc(sizeof(*p), GFP_KERNEL);
  821. if (!p)
  822. return;
  823. memset(p, 0, sizeof(*p));
  824. p->name = "Video ROM";
  825. p->start = mem_res->start + 0xc0000UL;
  826. p->end = p->start + 0x7fffUL;
  827. p->flags = IORESOURCE_BUSY;
  828. request_resource(mem_res, p);
  829. }
  830. /* Generic helper routines for PCI error reporting. */
  831. void pci_scan_for_target_abort(struct pci_controller_info *p,
  832. struct pci_pbm_info *pbm,
  833. struct pci_bus *pbus)
  834. {
  835. struct pci_dev *pdev;
  836. struct pci_bus *bus;
  837. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  838. u16 status, error_bits;
  839. pci_read_config_word(pdev, PCI_STATUS, &status);
  840. error_bits =
  841. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  842. PCI_STATUS_REC_TARGET_ABORT));
  843. if (error_bits) {
  844. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  845. printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
  846. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  847. pci_name(pdev), status);
  848. }
  849. }
  850. list_for_each_entry(bus, &pbus->children, node)
  851. pci_scan_for_target_abort(p, pbm, bus);
  852. }
  853. void pci_scan_for_master_abort(struct pci_controller_info *p,
  854. struct pci_pbm_info *pbm,
  855. struct pci_bus *pbus)
  856. {
  857. struct pci_dev *pdev;
  858. struct pci_bus *bus;
  859. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  860. u16 status, error_bits;
  861. pci_read_config_word(pdev, PCI_STATUS, &status);
  862. error_bits =
  863. (status & (PCI_STATUS_REC_MASTER_ABORT));
  864. if (error_bits) {
  865. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  866. printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
  867. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  868. pci_name(pdev), status);
  869. }
  870. }
  871. list_for_each_entry(bus, &pbus->children, node)
  872. pci_scan_for_master_abort(p, pbm, bus);
  873. }
  874. void pci_scan_for_parity_error(struct pci_controller_info *p,
  875. struct pci_pbm_info *pbm,
  876. struct pci_bus *pbus)
  877. {
  878. struct pci_dev *pdev;
  879. struct pci_bus *bus;
  880. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  881. u16 status, error_bits;
  882. pci_read_config_word(pdev, PCI_STATUS, &status);
  883. error_bits =
  884. (status & (PCI_STATUS_PARITY |
  885. PCI_STATUS_DETECTED_PARITY));
  886. if (error_bits) {
  887. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  888. printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
  889. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  890. pci_name(pdev), status);
  891. }
  892. }
  893. list_for_each_entry(bus, &pbus->children, node)
  894. pci_scan_for_parity_error(p, pbm, bus);
  895. }