pci_common.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:29k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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