pci.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /* pci.c: UltraSparc PCI controller support.
  2. *
  3. * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
  4. * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
  6. *
  7. * OF tree based PCI bus probing taken from the PowerPC port
  8. * with minor modifications, see there for credits.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/msi.h>
  18. #include <linux/irq.h>
  19. #include <linux/init.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pbm.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/irq.h>
  24. #include <asm/ebus.h>
  25. #include <asm/isa.h>
  26. #include <asm/prom.h>
  27. #include "pci_impl.h"
  28. unsigned long pci_memspace_mask = 0xffffffffUL;
  29. #ifndef CONFIG_PCI
  30. /* A "nop" PCI implementation. */
  31. asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
  32. unsigned long off, unsigned long len,
  33. unsigned char *buf)
  34. {
  35. return 0;
  36. }
  37. asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
  38. unsigned long off, unsigned long len,
  39. unsigned char *buf)
  40. {
  41. return 0;
  42. }
  43. #else
  44. /* List of all PCI controllers found in the system. */
  45. struct pci_controller_info *pci_controller_root = NULL;
  46. /* Each PCI controller found gets a unique index. */
  47. int pci_num_controllers = 0;
  48. volatile int pci_poke_in_progress;
  49. volatile int pci_poke_cpu = -1;
  50. volatile int pci_poke_faulted;
  51. static DEFINE_SPINLOCK(pci_poke_lock);
  52. void pci_config_read8(u8 *addr, u8 *ret)
  53. {
  54. unsigned long flags;
  55. u8 byte;
  56. spin_lock_irqsave(&pci_poke_lock, flags);
  57. pci_poke_cpu = smp_processor_id();
  58. pci_poke_in_progress = 1;
  59. pci_poke_faulted = 0;
  60. __asm__ __volatile__("membar #Sync\n\t"
  61. "lduba [%1] %2, %0\n\t"
  62. "membar #Sync"
  63. : "=r" (byte)
  64. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  65. : "memory");
  66. pci_poke_in_progress = 0;
  67. pci_poke_cpu = -1;
  68. if (!pci_poke_faulted)
  69. *ret = byte;
  70. spin_unlock_irqrestore(&pci_poke_lock, flags);
  71. }
  72. void pci_config_read16(u16 *addr, u16 *ret)
  73. {
  74. unsigned long flags;
  75. u16 word;
  76. spin_lock_irqsave(&pci_poke_lock, flags);
  77. pci_poke_cpu = smp_processor_id();
  78. pci_poke_in_progress = 1;
  79. pci_poke_faulted = 0;
  80. __asm__ __volatile__("membar #Sync\n\t"
  81. "lduha [%1] %2, %0\n\t"
  82. "membar #Sync"
  83. : "=r" (word)
  84. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  85. : "memory");
  86. pci_poke_in_progress = 0;
  87. pci_poke_cpu = -1;
  88. if (!pci_poke_faulted)
  89. *ret = word;
  90. spin_unlock_irqrestore(&pci_poke_lock, flags);
  91. }
  92. void pci_config_read32(u32 *addr, u32 *ret)
  93. {
  94. unsigned long flags;
  95. u32 dword;
  96. spin_lock_irqsave(&pci_poke_lock, flags);
  97. pci_poke_cpu = smp_processor_id();
  98. pci_poke_in_progress = 1;
  99. pci_poke_faulted = 0;
  100. __asm__ __volatile__("membar #Sync\n\t"
  101. "lduwa [%1] %2, %0\n\t"
  102. "membar #Sync"
  103. : "=r" (dword)
  104. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  105. : "memory");
  106. pci_poke_in_progress = 0;
  107. pci_poke_cpu = -1;
  108. if (!pci_poke_faulted)
  109. *ret = dword;
  110. spin_unlock_irqrestore(&pci_poke_lock, flags);
  111. }
  112. void pci_config_write8(u8 *addr, u8 val)
  113. {
  114. unsigned long flags;
  115. spin_lock_irqsave(&pci_poke_lock, flags);
  116. pci_poke_cpu = smp_processor_id();
  117. pci_poke_in_progress = 1;
  118. pci_poke_faulted = 0;
  119. __asm__ __volatile__("membar #Sync\n\t"
  120. "stba %0, [%1] %2\n\t"
  121. "membar #Sync"
  122. : /* no outputs */
  123. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  124. : "memory");
  125. pci_poke_in_progress = 0;
  126. pci_poke_cpu = -1;
  127. spin_unlock_irqrestore(&pci_poke_lock, flags);
  128. }
  129. void pci_config_write16(u16 *addr, u16 val)
  130. {
  131. unsigned long flags;
  132. spin_lock_irqsave(&pci_poke_lock, flags);
  133. pci_poke_cpu = smp_processor_id();
  134. pci_poke_in_progress = 1;
  135. pci_poke_faulted = 0;
  136. __asm__ __volatile__("membar #Sync\n\t"
  137. "stha %0, [%1] %2\n\t"
  138. "membar #Sync"
  139. : /* no outputs */
  140. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  141. : "memory");
  142. pci_poke_in_progress = 0;
  143. pci_poke_cpu = -1;
  144. spin_unlock_irqrestore(&pci_poke_lock, flags);
  145. }
  146. void pci_config_write32(u32 *addr, u32 val)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&pci_poke_lock, flags);
  150. pci_poke_cpu = smp_processor_id();
  151. pci_poke_in_progress = 1;
  152. pci_poke_faulted = 0;
  153. __asm__ __volatile__("membar #Sync\n\t"
  154. "stwa %0, [%1] %2\n\t"
  155. "membar #Sync"
  156. : /* no outputs */
  157. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  158. : "memory");
  159. pci_poke_in_progress = 0;
  160. pci_poke_cpu = -1;
  161. spin_unlock_irqrestore(&pci_poke_lock, flags);
  162. }
  163. /* Probe for all PCI controllers in the system. */
  164. extern void sabre_init(struct device_node *, const char *);
  165. extern void psycho_init(struct device_node *, const char *);
  166. extern void schizo_init(struct device_node *, const char *);
  167. extern void schizo_plus_init(struct device_node *, const char *);
  168. extern void tomatillo_init(struct device_node *, const char *);
  169. extern void sun4v_pci_init(struct device_node *, const char *);
  170. static struct {
  171. char *model_name;
  172. void (*init)(struct device_node *, const char *);
  173. } pci_controller_table[] __initdata = {
  174. { "SUNW,sabre", sabre_init },
  175. { "pci108e,a000", sabre_init },
  176. { "pci108e,a001", sabre_init },
  177. { "SUNW,psycho", psycho_init },
  178. { "pci108e,8000", psycho_init },
  179. { "SUNW,schizo", schizo_init },
  180. { "pci108e,8001", schizo_init },
  181. { "SUNW,schizo+", schizo_plus_init },
  182. { "pci108e,8002", schizo_plus_init },
  183. { "SUNW,tomatillo", tomatillo_init },
  184. { "pci108e,a801", tomatillo_init },
  185. { "SUNW,sun4v-pci", sun4v_pci_init },
  186. };
  187. #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
  188. sizeof(pci_controller_table[0]))
  189. static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
  190. {
  191. int i;
  192. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  193. if (!strncmp(model_name,
  194. pci_controller_table[i].model_name,
  195. namelen)) {
  196. pci_controller_table[i].init(dp, model_name);
  197. return 1;
  198. }
  199. }
  200. return 0;
  201. }
  202. static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
  203. {
  204. int i;
  205. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  206. if (!strncmp(model_name,
  207. pci_controller_table[i].model_name,
  208. namelen)) {
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
  215. {
  216. struct device_node *dp;
  217. int count = 0;
  218. for_each_node_by_name(dp, "pci") {
  219. struct property *prop;
  220. int len;
  221. prop = of_find_property(dp, "model", &len);
  222. if (!prop)
  223. prop = of_find_property(dp, "compatible", &len);
  224. if (prop) {
  225. const char *model = prop->value;
  226. int item_len = 0;
  227. /* Our value may be a multi-valued string in the
  228. * case of some compatible properties. For sanity,
  229. * only try the first one.
  230. */
  231. while (model[item_len] && len) {
  232. len--;
  233. item_len++;
  234. }
  235. if (handler(model, item_len, dp))
  236. count++;
  237. }
  238. }
  239. return count;
  240. }
  241. /* Is there some PCI controller in the system? */
  242. int __init pcic_present(void)
  243. {
  244. return pci_controller_scan(pci_is_controller);
  245. }
  246. struct pci_iommu_ops *pci_iommu_ops;
  247. EXPORT_SYMBOL(pci_iommu_ops);
  248. extern struct pci_iommu_ops pci_sun4u_iommu_ops,
  249. pci_sun4v_iommu_ops;
  250. /* Find each controller in the system, attach and initialize
  251. * software state structure for each and link into the
  252. * pci_controller_root. Setup the controller enough such
  253. * that bus scanning can be done.
  254. */
  255. static void __init pci_controller_probe(void)
  256. {
  257. if (tlb_type == hypervisor)
  258. pci_iommu_ops = &pci_sun4v_iommu_ops;
  259. else
  260. pci_iommu_ops = &pci_sun4u_iommu_ops;
  261. printk("PCI: Probing for controllers.\n");
  262. pci_controller_scan(pci_controller_init);
  263. }
  264. static unsigned long pci_parse_of_flags(u32 addr0)
  265. {
  266. unsigned long flags = 0;
  267. if (addr0 & 0x02000000) {
  268. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  269. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  270. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  271. if (addr0 & 0x40000000)
  272. flags |= IORESOURCE_PREFETCH
  273. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  274. } else if (addr0 & 0x01000000)
  275. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  276. return flags;
  277. }
  278. /* The of_device layer has translated all of the assigned-address properties
  279. * into physical address resources, we only have to figure out the register
  280. * mapping.
  281. */
  282. static void pci_parse_of_addrs(struct of_device *op,
  283. struct device_node *node,
  284. struct pci_dev *dev)
  285. {
  286. struct resource *op_res;
  287. const u32 *addrs;
  288. int proplen;
  289. addrs = of_get_property(node, "assigned-addresses", &proplen);
  290. if (!addrs)
  291. return;
  292. printk(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  293. op_res = &op->resource[0];
  294. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  295. struct resource *res;
  296. unsigned long flags;
  297. int i;
  298. flags = pci_parse_of_flags(addrs[0]);
  299. if (!flags)
  300. continue;
  301. i = addrs[0] & 0xff;
  302. printk(" start: %lx, end: %lx, i: %x\n",
  303. op_res->start, op_res->end, i);
  304. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  305. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  306. } else if (i == dev->rom_base_reg) {
  307. res = &dev->resource[PCI_ROM_RESOURCE];
  308. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  309. } else {
  310. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  311. continue;
  312. }
  313. res->start = op_res->start;
  314. res->end = op_res->end;
  315. res->flags = flags;
  316. res->name = pci_name(dev);
  317. }
  318. }
  319. struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  320. struct device_node *node,
  321. struct pci_bus *bus, int devfn)
  322. {
  323. struct dev_archdata *sd;
  324. struct pci_dev *dev;
  325. const char *type;
  326. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  327. if (!dev)
  328. return NULL;
  329. sd = &dev->dev.archdata;
  330. sd->iommu = pbm->iommu;
  331. sd->stc = &pbm->stc;
  332. sd->host_controller = pbm;
  333. sd->prom_node = node;
  334. sd->op = of_find_device_by_node(node);
  335. sd->msi_num = 0xffffffff;
  336. type = of_get_property(node, "device_type", NULL);
  337. if (type == NULL)
  338. type = "";
  339. printk(" create device, devfn: %x, type: %s\n", devfn, type);
  340. dev->bus = bus;
  341. dev->sysdata = node;
  342. dev->dev.parent = bus->bridge;
  343. dev->dev.bus = &pci_bus_type;
  344. dev->devfn = devfn;
  345. dev->multifunction = 0; /* maybe a lie? */
  346. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  347. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  348. dev->subsystem_vendor =
  349. of_getintprop_default(node, "subsystem-vendor-id", 0);
  350. dev->subsystem_device =
  351. of_getintprop_default(node, "subsystem-id", 0);
  352. dev->cfg_size = pci_cfg_space_size(dev);
  353. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  354. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  355. dev->class = of_getintprop_default(node, "class-code", 0);
  356. printk(" class: 0x%x\n", dev->class);
  357. dev->current_state = 4; /* unknown power state */
  358. dev->error_state = pci_channel_io_normal;
  359. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  360. /* a PCI-PCI bridge */
  361. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  362. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  363. } else if (!strcmp(type, "cardbus")) {
  364. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  365. } else {
  366. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  367. dev->rom_base_reg = PCI_ROM_ADDRESS;
  368. dev->irq = sd->op->irqs[0];
  369. if (dev->irq == 0xffffffff)
  370. dev->irq = PCI_IRQ_NONE;
  371. }
  372. pci_parse_of_addrs(sd->op, node, dev);
  373. printk(" adding to system ...\n");
  374. pci_device_add(dev, bus);
  375. return dev;
  376. }
  377. static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
  378. struct device_node *node,
  379. struct pci_bus *bus);
  380. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  381. void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  382. struct device_node *node,
  383. struct pci_dev *dev)
  384. {
  385. struct pci_bus *bus;
  386. const u32 *busrange, *ranges;
  387. int len, i;
  388. struct resource *res;
  389. unsigned int flags;
  390. u64 size;
  391. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  392. /* parse bus-range property */
  393. busrange = of_get_property(node, "bus-range", &len);
  394. if (busrange == NULL || len != 8) {
  395. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  396. node->full_name);
  397. return;
  398. }
  399. ranges = of_get_property(node, "ranges", &len);
  400. if (ranges == NULL) {
  401. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  402. node->full_name);
  403. return;
  404. }
  405. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  406. if (!bus) {
  407. printk(KERN_ERR "Failed to create pci bus for %s\n",
  408. node->full_name);
  409. return;
  410. }
  411. bus->primary = dev->bus->number;
  412. bus->subordinate = busrange[1];
  413. bus->bridge_ctl = 0;
  414. /* parse ranges property */
  415. /* PCI #address-cells == 3 and #size-cells == 2 always */
  416. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  417. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  418. res->flags = 0;
  419. bus->resource[i] = res;
  420. ++res;
  421. }
  422. i = 1;
  423. for (; len >= 32; len -= 32, ranges += 8) {
  424. struct resource *root;
  425. flags = pci_parse_of_flags(ranges[0]);
  426. size = GET_64BIT(ranges, 6);
  427. if (flags == 0 || size == 0)
  428. continue;
  429. if (flags & IORESOURCE_IO) {
  430. res = bus->resource[0];
  431. if (res->flags) {
  432. printk(KERN_ERR "PCI: ignoring extra I/O range"
  433. " for bridge %s\n", node->full_name);
  434. continue;
  435. }
  436. root = &pbm->io_space;
  437. } else {
  438. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  439. printk(KERN_ERR "PCI: too many memory ranges"
  440. " for bridge %s\n", node->full_name);
  441. continue;
  442. }
  443. res = bus->resource[i];
  444. ++i;
  445. root = &pbm->mem_space;
  446. }
  447. res->start = GET_64BIT(ranges, 1);
  448. res->end = res->start + size - 1;
  449. res->flags = flags;
  450. /* Another way to implement this would be to add an of_device
  451. * layer routine that can calculate a resource for a given
  452. * range property value in a PCI device.
  453. */
  454. pbm->parent->resource_adjust(dev, res, root);
  455. }
  456. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  457. bus->number);
  458. printk(" bus name: %s\n", bus->name);
  459. pci_of_scan_bus(pbm, node, bus);
  460. }
  461. static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
  462. struct device_node *node,
  463. struct pci_bus *bus)
  464. {
  465. struct device_node *child;
  466. const u32 *reg;
  467. int reglen, devfn;
  468. struct pci_dev *dev;
  469. printk("PCI: scan_bus[%s] bus no %d\n",
  470. node->full_name, bus->number);
  471. child = NULL;
  472. while ((child = of_get_next_child(node, child)) != NULL) {
  473. printk(" * %s\n", child->full_name);
  474. reg = of_get_property(child, "reg", &reglen);
  475. if (reg == NULL || reglen < 20)
  476. continue;
  477. devfn = (reg[0] >> 8) & 0xff;
  478. /* create a new pci_dev for this device */
  479. dev = of_create_pci_dev(pbm, child, bus, devfn);
  480. if (!dev)
  481. continue;
  482. printk("PCI: dev header type: %x\n", dev->hdr_type);
  483. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  484. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  485. of_scan_pci_bridge(pbm, child, dev);
  486. }
  487. }
  488. static ssize_t
  489. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  490. {
  491. struct pci_dev *pdev;
  492. struct device_node *dp;
  493. pdev = to_pci_dev(dev);
  494. dp = pdev->dev.archdata.prom_node;
  495. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  496. }
  497. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  498. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  499. {
  500. struct pci_dev *dev;
  501. int err;
  502. list_for_each_entry(dev, &bus->devices, bus_list) {
  503. /* we don't really care if we can create this file or
  504. * not, but we need to assign the result of the call
  505. * or the world will fall under alien invasion and
  506. * everybody will be frozen on a spaceship ready to be
  507. * eaten on alpha centauri by some green and jelly
  508. * humanoid.
  509. */
  510. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  511. }
  512. }
  513. struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
  514. {
  515. struct pci_controller_info *p = pbm->parent;
  516. struct device_node *node = pbm->prom_node;
  517. struct pci_bus *bus;
  518. printk("PCI: Scanning PBM %s\n", node->full_name);
  519. /* XXX parent device? XXX */
  520. bus = pci_create_bus(NULL, pbm->pci_first_busno, p->pci_ops, pbm);
  521. if (!bus) {
  522. printk(KERN_ERR "Failed to create bus for %s\n",
  523. node->full_name);
  524. return NULL;
  525. }
  526. bus->secondary = pbm->pci_first_busno;
  527. bus->subordinate = pbm->pci_last_busno;
  528. bus->resource[0] = &pbm->io_space;
  529. bus->resource[1] = &pbm->mem_space;
  530. pci_of_scan_bus(pbm, node, bus);
  531. pci_bus_add_devices(bus);
  532. pci_bus_register_of_sysfs(bus);
  533. return bus;
  534. }
  535. static void __init pci_scan_each_controller_bus(void)
  536. {
  537. struct pci_controller_info *p;
  538. for (p = pci_controller_root; p; p = p->next)
  539. p->scan_bus(p);
  540. }
  541. extern void power_init(void);
  542. static int __init pcibios_init(void)
  543. {
  544. pci_controller_probe();
  545. if (pci_controller_root == NULL)
  546. return 0;
  547. pci_scan_each_controller_bus();
  548. isa_init();
  549. ebus_init();
  550. power_init();
  551. return 0;
  552. }
  553. subsys_initcall(pcibios_init);
  554. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  555. {
  556. struct pci_pbm_info *pbm = pbus->sysdata;
  557. /* Generic PCI bus probing sets these to point at
  558. * &io{port,mem}_resouce which is wrong for us.
  559. */
  560. pbus->resource[0] = &pbm->io_space;
  561. pbus->resource[1] = &pbm->mem_space;
  562. }
  563. struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
  564. {
  565. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  566. struct resource *root = NULL;
  567. if (r->flags & IORESOURCE_IO)
  568. root = &pbm->io_space;
  569. if (r->flags & IORESOURCE_MEM)
  570. root = &pbm->mem_space;
  571. return root;
  572. }
  573. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  574. {
  575. }
  576. void pcibios_align_resource(void *data, struct resource *res,
  577. resource_size_t size, resource_size_t align)
  578. {
  579. }
  580. int pcibios_enable_device(struct pci_dev *dev, int mask)
  581. {
  582. u16 cmd, oldcmd;
  583. int i;
  584. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  585. oldcmd = cmd;
  586. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  587. struct resource *res = &dev->resource[i];
  588. /* Only set up the requested stuff */
  589. if (!(mask & (1<<i)))
  590. continue;
  591. if (res->flags & IORESOURCE_IO)
  592. cmd |= PCI_COMMAND_IO;
  593. if (res->flags & IORESOURCE_MEM)
  594. cmd |= PCI_COMMAND_MEMORY;
  595. }
  596. if (cmd != oldcmd) {
  597. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  598. pci_name(dev), cmd);
  599. /* Enable the appropriate bits in the PCI command register. */
  600. pci_write_config_word(dev, PCI_COMMAND, cmd);
  601. }
  602. return 0;
  603. }
  604. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  605. struct resource *res)
  606. {
  607. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  608. struct resource zero_res, *root;
  609. zero_res.start = 0;
  610. zero_res.end = 0;
  611. zero_res.flags = res->flags;
  612. if (res->flags & IORESOURCE_IO)
  613. root = &pbm->io_space;
  614. else
  615. root = &pbm->mem_space;
  616. pbm->parent->resource_adjust(pdev, &zero_res, root);
  617. region->start = res->start - zero_res.start;
  618. region->end = res->end - zero_res.start;
  619. }
  620. EXPORT_SYMBOL(pcibios_resource_to_bus);
  621. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  622. struct pci_bus_region *region)
  623. {
  624. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  625. struct resource *root;
  626. res->start = region->start;
  627. res->end = region->end;
  628. if (res->flags & IORESOURCE_IO)
  629. root = &pbm->io_space;
  630. else
  631. root = &pbm->mem_space;
  632. pbm->parent->resource_adjust(pdev, res, root);
  633. }
  634. EXPORT_SYMBOL(pcibios_bus_to_resource);
  635. char * __devinit pcibios_setup(char *str)
  636. {
  637. return str;
  638. }
  639. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  640. /* If the user uses a host-bridge as the PCI device, he may use
  641. * this to perform a raw mmap() of the I/O or MEM space behind
  642. * that controller.
  643. *
  644. * This can be useful for execution of x86 PCI bios initialization code
  645. * on a PCI card, like the xfree86 int10 stuff does.
  646. */
  647. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  648. enum pci_mmap_state mmap_state)
  649. {
  650. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  651. struct pci_controller_info *p;
  652. unsigned long space_size, user_offset, user_size;
  653. p = pbm->parent;
  654. if (p->pbms_same_domain) {
  655. unsigned long lowest, highest;
  656. lowest = ~0UL; highest = 0UL;
  657. if (mmap_state == pci_mmap_io) {
  658. if (p->pbm_A.io_space.flags) {
  659. lowest = p->pbm_A.io_space.start;
  660. highest = p->pbm_A.io_space.end + 1;
  661. }
  662. if (p->pbm_B.io_space.flags) {
  663. if (lowest > p->pbm_B.io_space.start)
  664. lowest = p->pbm_B.io_space.start;
  665. if (highest < p->pbm_B.io_space.end + 1)
  666. highest = p->pbm_B.io_space.end + 1;
  667. }
  668. space_size = highest - lowest;
  669. } else {
  670. if (p->pbm_A.mem_space.flags) {
  671. lowest = p->pbm_A.mem_space.start;
  672. highest = p->pbm_A.mem_space.end + 1;
  673. }
  674. if (p->pbm_B.mem_space.flags) {
  675. if (lowest > p->pbm_B.mem_space.start)
  676. lowest = p->pbm_B.mem_space.start;
  677. if (highest < p->pbm_B.mem_space.end + 1)
  678. highest = p->pbm_B.mem_space.end + 1;
  679. }
  680. space_size = highest - lowest;
  681. }
  682. } else {
  683. if (mmap_state == pci_mmap_io) {
  684. space_size = (pbm->io_space.end -
  685. pbm->io_space.start) + 1;
  686. } else {
  687. space_size = (pbm->mem_space.end -
  688. pbm->mem_space.start) + 1;
  689. }
  690. }
  691. /* Make sure the request is in range. */
  692. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  693. user_size = vma->vm_end - vma->vm_start;
  694. if (user_offset >= space_size ||
  695. (user_offset + user_size) > space_size)
  696. return -EINVAL;
  697. if (p->pbms_same_domain) {
  698. unsigned long lowest = ~0UL;
  699. if (mmap_state == pci_mmap_io) {
  700. if (p->pbm_A.io_space.flags)
  701. lowest = p->pbm_A.io_space.start;
  702. if (p->pbm_B.io_space.flags &&
  703. lowest > p->pbm_B.io_space.start)
  704. lowest = p->pbm_B.io_space.start;
  705. } else {
  706. if (p->pbm_A.mem_space.flags)
  707. lowest = p->pbm_A.mem_space.start;
  708. if (p->pbm_B.mem_space.flags &&
  709. lowest > p->pbm_B.mem_space.start)
  710. lowest = p->pbm_B.mem_space.start;
  711. }
  712. vma->vm_pgoff = (lowest + user_offset) >> PAGE_SHIFT;
  713. } else {
  714. if (mmap_state == pci_mmap_io) {
  715. vma->vm_pgoff = (pbm->io_space.start +
  716. user_offset) >> PAGE_SHIFT;
  717. } else {
  718. vma->vm_pgoff = (pbm->mem_space.start +
  719. user_offset) >> PAGE_SHIFT;
  720. }
  721. }
  722. return 0;
  723. }
  724. /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
  725. * to the 32-bit pci bus offset for DEV requested by the user.
  726. *
  727. * Basically, the user finds the base address for his device which he wishes
  728. * to mmap. They read the 32-bit value from the config space base register,
  729. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  730. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  731. *
  732. * Returns negative error code on failure, zero on success.
  733. */
  734. static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  735. enum pci_mmap_state mmap_state)
  736. {
  737. unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
  738. unsigned long user32 = user_offset & pci_memspace_mask;
  739. unsigned long largest_base, this_base, addr32;
  740. int i;
  741. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  742. return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
  743. /* Figure out which base address this is for. */
  744. largest_base = 0UL;
  745. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  746. struct resource *rp = &dev->resource[i];
  747. /* Active? */
  748. if (!rp->flags)
  749. continue;
  750. /* Same type? */
  751. if (i == PCI_ROM_RESOURCE) {
  752. if (mmap_state != pci_mmap_mem)
  753. continue;
  754. } else {
  755. if ((mmap_state == pci_mmap_io &&
  756. (rp->flags & IORESOURCE_IO) == 0) ||
  757. (mmap_state == pci_mmap_mem &&
  758. (rp->flags & IORESOURCE_MEM) == 0))
  759. continue;
  760. }
  761. this_base = rp->start;
  762. addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
  763. if (mmap_state == pci_mmap_io)
  764. addr32 &= 0xffffff;
  765. if (addr32 <= user32 && this_base > largest_base)
  766. largest_base = this_base;
  767. }
  768. if (largest_base == 0UL)
  769. return -EINVAL;
  770. /* Now construct the final physical address. */
  771. if (mmap_state == pci_mmap_io)
  772. vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
  773. else
  774. vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
  775. return 0;
  776. }
  777. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  778. * mapping.
  779. */
  780. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  781. enum pci_mmap_state mmap_state)
  782. {
  783. vma->vm_flags |= (VM_IO | VM_RESERVED);
  784. }
  785. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  786. * device mapping.
  787. */
  788. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  789. enum pci_mmap_state mmap_state)
  790. {
  791. /* Our io_remap_pfn_range takes care of this, do nothing. */
  792. }
  793. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  794. * for this architecture. The region in the process to map is described by vm_start
  795. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  796. * The pci device structure is provided so that architectures may make mapping
  797. * decisions on a per-device or per-bus basis.
  798. *
  799. * Returns a negative error code on failure, zero on success.
  800. */
  801. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  802. enum pci_mmap_state mmap_state,
  803. int write_combine)
  804. {
  805. int ret;
  806. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  807. if (ret < 0)
  808. return ret;
  809. __pci_mmap_set_flags(dev, vma, mmap_state);
  810. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  811. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  812. ret = io_remap_pfn_range(vma, vma->vm_start,
  813. vma->vm_pgoff,
  814. vma->vm_end - vma->vm_start,
  815. vma->vm_page_prot);
  816. if (ret)
  817. return ret;
  818. return 0;
  819. }
  820. /* Return the domain nuber for this pci bus */
  821. int pci_domain_nr(struct pci_bus *pbus)
  822. {
  823. struct pci_pbm_info *pbm = pbus->sysdata;
  824. int ret;
  825. if (pbm == NULL || pbm->parent == NULL) {
  826. ret = -ENXIO;
  827. } else {
  828. struct pci_controller_info *p = pbm->parent;
  829. ret = p->index;
  830. if (p->pbms_same_domain == 0)
  831. ret = ((ret << 1) +
  832. ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
  833. }
  834. return ret;
  835. }
  836. EXPORT_SYMBOL(pci_domain_nr);
  837. #ifdef CONFIG_PCI_MSI
  838. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  839. {
  840. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  841. struct pci_controller_info *p = pbm->parent;
  842. int virt_irq, err;
  843. if (!pbm->msi_num || !p->setup_msi_irq)
  844. return -EINVAL;
  845. err = p->setup_msi_irq(&virt_irq, pdev, desc);
  846. if (err < 0)
  847. return err;
  848. return virt_irq;
  849. }
  850. void arch_teardown_msi_irq(unsigned int virt_irq)
  851. {
  852. struct msi_desc *entry = get_irq_msi(virt_irq);
  853. struct pci_dev *pdev = entry->dev;
  854. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  855. struct pci_controller_info *p = pbm->parent;
  856. if (!pbm->msi_num || !p->setup_msi_irq)
  857. return;
  858. return p->teardown_msi_irq(virt_irq, pdev);
  859. }
  860. #endif /* !(CONFIG_PCI_MSI) */
  861. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  862. {
  863. return pdev->dev.archdata.prom_node;
  864. }
  865. EXPORT_SYMBOL(pci_device_to_OF_node);
  866. #endif /* !(CONFIG_PCI) */