pci.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/pci.h>
  17. #include <linux/msi.h>
  18. #include <linux/irq.h>
  19. #include <linux/init.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/irq.h>
  23. #include <asm/ebus.h>
  24. #include <asm/isa.h>
  25. #include <asm/prom.h>
  26. #include <asm/apb.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_pbm_info *pci_pbm_root = NULL;
  46. /* Each PBM found gets a unique index. */
  47. int pci_num_pbms = 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. extern void fire_pci_init(struct device_node *, const char *);
  171. static struct {
  172. char *model_name;
  173. void (*init)(struct device_node *, const char *);
  174. } pci_controller_table[] __initdata = {
  175. { "SUNW,sabre", sabre_init },
  176. { "pci108e,a000", sabre_init },
  177. { "pci108e,a001", sabre_init },
  178. { "SUNW,psycho", psycho_init },
  179. { "pci108e,8000", psycho_init },
  180. { "SUNW,schizo", schizo_init },
  181. { "pci108e,8001", schizo_init },
  182. { "SUNW,schizo+", schizo_plus_init },
  183. { "pci108e,8002", schizo_plus_init },
  184. { "SUNW,tomatillo", tomatillo_init },
  185. { "pci108e,a801", tomatillo_init },
  186. { "SUNW,sun4v-pci", sun4v_pci_init },
  187. { "pciex108e,80f0", fire_pci_init },
  188. };
  189. #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
  190. sizeof(pci_controller_table[0]))
  191. static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
  192. {
  193. int i;
  194. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  195. if (!strncmp(model_name,
  196. pci_controller_table[i].model_name,
  197. namelen)) {
  198. pci_controller_table[i].init(dp, model_name);
  199. return 1;
  200. }
  201. }
  202. return 0;
  203. }
  204. static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
  205. {
  206. int i;
  207. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  208. if (!strncmp(model_name,
  209. pci_controller_table[i].model_name,
  210. namelen)) {
  211. return 1;
  212. }
  213. }
  214. return 0;
  215. }
  216. static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
  217. {
  218. struct device_node *dp;
  219. int count = 0;
  220. for_each_node_by_name(dp, "pci") {
  221. struct property *prop;
  222. int len;
  223. prop = of_find_property(dp, "model", &len);
  224. if (!prop)
  225. prop = of_find_property(dp, "compatible", &len);
  226. if (prop) {
  227. const char *model = prop->value;
  228. int item_len = 0;
  229. /* Our value may be a multi-valued string in the
  230. * case of some compatible properties. For sanity,
  231. * only try the first one.
  232. */
  233. while (model[item_len] && len) {
  234. len--;
  235. item_len++;
  236. }
  237. if (handler(model, item_len, dp))
  238. count++;
  239. }
  240. }
  241. return count;
  242. }
  243. /* Is there some PCI controller in the system? */
  244. int __init pcic_present(void)
  245. {
  246. return pci_controller_scan(pci_is_controller);
  247. }
  248. const struct pci_iommu_ops *pci_iommu_ops;
  249. EXPORT_SYMBOL(pci_iommu_ops);
  250. extern const struct pci_iommu_ops pci_sun4u_iommu_ops,
  251. pci_sun4v_iommu_ops;
  252. /* Find each controller in the system, attach and initialize
  253. * software state structure for each and link into the
  254. * pci_pbm_root. Setup the controller enough such
  255. * that bus scanning can be done.
  256. */
  257. static void __init pci_controller_probe(void)
  258. {
  259. if (tlb_type == hypervisor)
  260. pci_iommu_ops = &pci_sun4v_iommu_ops;
  261. else
  262. pci_iommu_ops = &pci_sun4u_iommu_ops;
  263. printk("PCI: Probing for controllers.\n");
  264. pci_controller_scan(pci_controller_init);
  265. }
  266. static unsigned long pci_parse_of_flags(u32 addr0)
  267. {
  268. unsigned long flags = 0;
  269. if (addr0 & 0x02000000) {
  270. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  271. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  272. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  273. if (addr0 & 0x40000000)
  274. flags |= IORESOURCE_PREFETCH
  275. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  276. } else if (addr0 & 0x01000000)
  277. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  278. return flags;
  279. }
  280. /* The of_device layer has translated all of the assigned-address properties
  281. * into physical address resources, we only have to figure out the register
  282. * mapping.
  283. */
  284. static void pci_parse_of_addrs(struct of_device *op,
  285. struct device_node *node,
  286. struct pci_dev *dev)
  287. {
  288. struct resource *op_res;
  289. const u32 *addrs;
  290. int proplen;
  291. addrs = of_get_property(node, "assigned-addresses", &proplen);
  292. if (!addrs)
  293. return;
  294. printk(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  295. op_res = &op->resource[0];
  296. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  297. struct resource *res;
  298. unsigned long flags;
  299. int i;
  300. flags = pci_parse_of_flags(addrs[0]);
  301. if (!flags)
  302. continue;
  303. i = addrs[0] & 0xff;
  304. printk(" start: %lx, end: %lx, i: %x\n",
  305. op_res->start, op_res->end, i);
  306. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  307. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  308. } else if (i == dev->rom_base_reg) {
  309. res = &dev->resource[PCI_ROM_RESOURCE];
  310. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  311. } else {
  312. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  313. continue;
  314. }
  315. res->start = op_res->start;
  316. res->end = op_res->end;
  317. res->flags = flags;
  318. res->name = pci_name(dev);
  319. }
  320. }
  321. struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  322. struct device_node *node,
  323. struct pci_bus *bus, int devfn,
  324. int host_controller)
  325. {
  326. struct dev_archdata *sd;
  327. struct pci_dev *dev;
  328. const char *type;
  329. u32 class;
  330. dev = alloc_pci_dev();
  331. if (!dev)
  332. return NULL;
  333. sd = &dev->dev.archdata;
  334. sd->iommu = pbm->iommu;
  335. sd->stc = &pbm->stc;
  336. sd->host_controller = pbm;
  337. sd->prom_node = node;
  338. sd->op = of_find_device_by_node(node);
  339. sd->msi_num = 0xffffffff;
  340. type = of_get_property(node, "device_type", NULL);
  341. if (type == NULL)
  342. type = "";
  343. printk(" create device, devfn: %x, type: %s hostcontroller(%d)\n",
  344. devfn, type, host_controller);
  345. dev->bus = bus;
  346. dev->sysdata = node;
  347. dev->dev.parent = bus->bridge;
  348. dev->dev.bus = &pci_bus_type;
  349. dev->devfn = devfn;
  350. dev->multifunction = 0; /* maybe a lie? */
  351. if (host_controller) {
  352. dev->vendor = 0x108e;
  353. dev->device = 0x8000;
  354. dev->subsystem_vendor = 0x0000;
  355. dev->subsystem_device = 0x0000;
  356. dev->cfg_size = 256;
  357. dev->class = PCI_CLASS_BRIDGE_HOST << 8;
  358. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  359. 0x00, PCI_SLOT(devfn), PCI_FUNC(devfn));
  360. } else {
  361. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  362. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  363. dev->subsystem_vendor =
  364. of_getintprop_default(node, "subsystem-vendor-id", 0);
  365. dev->subsystem_device =
  366. of_getintprop_default(node, "subsystem-id", 0);
  367. dev->cfg_size = pci_cfg_space_size(dev);
  368. /* We can't actually use the firmware value, we have
  369. * to read what is in the register right now. One
  370. * reason is that in the case of IDE interfaces the
  371. * firmware can sample the value before the the IDE
  372. * interface is programmed into native mode.
  373. */
  374. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  375. dev->class = class >> 8;
  376. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  377. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  378. }
  379. printk(" class: 0x%x device name: %s\n",
  380. dev->class, pci_name(dev));
  381. /* I have seen IDE devices which will not respond to
  382. * the bmdma simplex check reads if bus mastering is
  383. * disabled.
  384. */
  385. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  386. pci_set_master(dev);
  387. dev->current_state = 4; /* unknown power state */
  388. dev->error_state = pci_channel_io_normal;
  389. if (host_controller) {
  390. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  391. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  392. dev->irq = PCI_IRQ_NONE;
  393. } else {
  394. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  395. /* a PCI-PCI bridge */
  396. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  397. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  398. } else if (!strcmp(type, "cardbus")) {
  399. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  400. } else {
  401. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  402. dev->rom_base_reg = PCI_ROM_ADDRESS;
  403. dev->irq = sd->op->irqs[0];
  404. if (dev->irq == 0xffffffff)
  405. dev->irq = PCI_IRQ_NONE;
  406. }
  407. }
  408. pci_parse_of_addrs(sd->op, node, dev);
  409. printk(" adding to system ...\n");
  410. pci_device_add(dev, bus);
  411. return dev;
  412. }
  413. static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  414. {
  415. u32 idx, first, last;
  416. first = 8;
  417. last = 0;
  418. for (idx = 0; idx < 8; idx++) {
  419. if ((map & (1 << idx)) != 0) {
  420. if (first > idx)
  421. first = idx;
  422. if (last < idx)
  423. last = idx;
  424. }
  425. }
  426. *first_p = first;
  427. *last_p = last;
  428. }
  429. static void pci_resource_adjust(struct resource *res,
  430. struct resource *root)
  431. {
  432. res->start += root->start;
  433. res->end += root->start;
  434. }
  435. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  436. * a proper 'ranges' property.
  437. */
  438. static void __devinit apb_fake_ranges(struct pci_dev *dev,
  439. struct pci_bus *bus,
  440. struct pci_pbm_info *pbm)
  441. {
  442. struct resource *res;
  443. u32 first, last;
  444. u8 map;
  445. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  446. apb_calc_first_last(map, &first, &last);
  447. res = bus->resource[0];
  448. res->start = (first << 21);
  449. res->end = (last << 21) + ((1 << 21) - 1);
  450. res->flags = IORESOURCE_IO;
  451. pci_resource_adjust(res, &pbm->io_space);
  452. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  453. apb_calc_first_last(map, &first, &last);
  454. res = bus->resource[1];
  455. res->start = (first << 21);
  456. res->end = (last << 21) + ((1 << 21) - 1);
  457. res->flags = IORESOURCE_MEM;
  458. pci_resource_adjust(res, &pbm->mem_space);
  459. }
  460. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  461. struct device_node *node,
  462. struct pci_bus *bus);
  463. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  464. static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  465. struct device_node *node,
  466. struct pci_dev *dev)
  467. {
  468. struct pci_bus *bus;
  469. const u32 *busrange, *ranges;
  470. int len, i, simba;
  471. struct resource *res;
  472. unsigned int flags;
  473. u64 size;
  474. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  475. /* parse bus-range property */
  476. busrange = of_get_property(node, "bus-range", &len);
  477. if (busrange == NULL || len != 8) {
  478. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  479. node->full_name);
  480. return;
  481. }
  482. ranges = of_get_property(node, "ranges", &len);
  483. simba = 0;
  484. if (ranges == NULL) {
  485. const char *model = of_get_property(node, "model", NULL);
  486. if (model && !strcmp(model, "SUNW,simba")) {
  487. simba = 1;
  488. } else {
  489. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  490. node->full_name);
  491. return;
  492. }
  493. }
  494. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  495. if (!bus) {
  496. printk(KERN_ERR "Failed to create pci bus for %s\n",
  497. node->full_name);
  498. return;
  499. }
  500. bus->primary = dev->bus->number;
  501. bus->subordinate = busrange[1];
  502. bus->bridge_ctl = 0;
  503. /* parse ranges property, or cook one up by hand for Simba */
  504. /* PCI #address-cells == 3 and #size-cells == 2 always */
  505. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  506. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  507. res->flags = 0;
  508. bus->resource[i] = res;
  509. ++res;
  510. }
  511. if (simba) {
  512. apb_fake_ranges(dev, bus, pbm);
  513. goto simba_cont;
  514. }
  515. i = 1;
  516. for (; len >= 32; len -= 32, ranges += 8) {
  517. struct resource *root;
  518. flags = pci_parse_of_flags(ranges[0]);
  519. size = GET_64BIT(ranges, 6);
  520. if (flags == 0 || size == 0)
  521. continue;
  522. if (flags & IORESOURCE_IO) {
  523. res = bus->resource[0];
  524. if (res->flags) {
  525. printk(KERN_ERR "PCI: ignoring extra I/O range"
  526. " for bridge %s\n", node->full_name);
  527. continue;
  528. }
  529. root = &pbm->io_space;
  530. } else {
  531. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  532. printk(KERN_ERR "PCI: too many memory ranges"
  533. " for bridge %s\n", node->full_name);
  534. continue;
  535. }
  536. res = bus->resource[i];
  537. ++i;
  538. root = &pbm->mem_space;
  539. }
  540. res->start = GET_64BIT(ranges, 1);
  541. res->end = res->start + size - 1;
  542. res->flags = flags;
  543. /* Another way to implement this would be to add an of_device
  544. * layer routine that can calculate a resource for a given
  545. * range property value in a PCI device.
  546. */
  547. pci_resource_adjust(res, root);
  548. }
  549. simba_cont:
  550. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  551. bus->number);
  552. printk(" bus name: %s\n", bus->name);
  553. pci_of_scan_bus(pbm, node, bus);
  554. }
  555. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  556. struct device_node *node,
  557. struct pci_bus *bus)
  558. {
  559. struct device_node *child;
  560. const u32 *reg;
  561. int reglen, devfn;
  562. struct pci_dev *dev;
  563. printk("PCI: scan_bus[%s] bus no %d\n",
  564. node->full_name, bus->number);
  565. child = NULL;
  566. while ((child = of_get_next_child(node, child)) != NULL) {
  567. printk(" * %s\n", child->full_name);
  568. reg = of_get_property(child, "reg", &reglen);
  569. if (reg == NULL || reglen < 20)
  570. continue;
  571. devfn = (reg[0] >> 8) & 0xff;
  572. /* create a new pci_dev for this device */
  573. dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
  574. if (!dev)
  575. continue;
  576. printk("PCI: dev header type: %x\n", dev->hdr_type);
  577. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  578. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  579. of_scan_pci_bridge(pbm, child, dev);
  580. }
  581. }
  582. static ssize_t
  583. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  584. {
  585. struct pci_dev *pdev;
  586. struct device_node *dp;
  587. pdev = to_pci_dev(dev);
  588. dp = pdev->dev.archdata.prom_node;
  589. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  590. }
  591. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  592. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  593. {
  594. struct pci_dev *dev;
  595. struct pci_bus *child_bus;
  596. int err;
  597. list_for_each_entry(dev, &bus->devices, bus_list) {
  598. /* we don't really care if we can create this file or
  599. * not, but we need to assign the result of the call
  600. * or the world will fall under alien invasion and
  601. * everybody will be frozen on a spaceship ready to be
  602. * eaten on alpha centauri by some green and jelly
  603. * humanoid.
  604. */
  605. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  606. }
  607. list_for_each_entry(child_bus, &bus->children, node)
  608. pci_bus_register_of_sysfs(child_bus);
  609. }
  610. int pci_host_bridge_read_pci_cfg(struct pci_bus *bus_dev,
  611. unsigned int devfn,
  612. int where, int size,
  613. u32 *value)
  614. {
  615. static u8 fake_pci_config[] = {
  616. 0x8e, 0x10, /* Vendor: 0x108e (Sun) */
  617. 0x00, 0x80, /* Device: 0x8000 (PBM) */
  618. 0x46, 0x01, /* Command: 0x0146 (SERR, PARITY, MASTER, MEM) */
  619. 0xa0, 0x22, /* Status: 0x02a0 (DEVSEL_MED, FB2B, 66MHZ) */
  620. 0x00, 0x00, 0x00, 0x06, /* Class: 0x06000000 host bridge */
  621. 0x00, /* Cacheline: 0x00 */
  622. 0x40, /* Latency: 0x40 */
  623. 0x00, /* Header-Type: 0x00 normal */
  624. };
  625. *value = 0;
  626. if (where >= 0 && where < sizeof(fake_pci_config) &&
  627. (where + size) >= 0 &&
  628. (where + size) < sizeof(fake_pci_config) &&
  629. size <= sizeof(u32)) {
  630. while (size--) {
  631. *value <<= 8;
  632. *value |= fake_pci_config[where + size];
  633. }
  634. }
  635. return PCIBIOS_SUCCESSFUL;
  636. }
  637. int pci_host_bridge_write_pci_cfg(struct pci_bus *bus_dev,
  638. unsigned int devfn,
  639. int where, int size,
  640. u32 value)
  641. {
  642. return PCIBIOS_SUCCESSFUL;
  643. }
  644. struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
  645. {
  646. struct device_node *node = pbm->prom_node;
  647. struct pci_dev *host_pdev;
  648. struct pci_bus *bus;
  649. printk("PCI: Scanning PBM %s\n", node->full_name);
  650. /* XXX parent device? XXX */
  651. bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
  652. if (!bus) {
  653. printk(KERN_ERR "Failed to create bus for %s\n",
  654. node->full_name);
  655. return NULL;
  656. }
  657. bus->secondary = pbm->pci_first_busno;
  658. bus->subordinate = pbm->pci_last_busno;
  659. bus->resource[0] = &pbm->io_space;
  660. bus->resource[1] = &pbm->mem_space;
  661. /* Create the dummy host bridge and link it in. */
  662. host_pdev = of_create_pci_dev(pbm, node, bus, 0x00, 1);
  663. bus->self = host_pdev;
  664. pci_of_scan_bus(pbm, node, bus);
  665. pci_bus_add_devices(bus);
  666. pci_bus_register_of_sysfs(bus);
  667. return bus;
  668. }
  669. static void __init pci_scan_each_controller_bus(void)
  670. {
  671. struct pci_pbm_info *pbm;
  672. for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
  673. pbm->scan_bus(pbm);
  674. }
  675. extern void power_init(void);
  676. static int __init pcibios_init(void)
  677. {
  678. pci_controller_probe();
  679. if (pci_pbm_root == NULL)
  680. return 0;
  681. pci_scan_each_controller_bus();
  682. isa_init();
  683. ebus_init();
  684. power_init();
  685. return 0;
  686. }
  687. subsys_initcall(pcibios_init);
  688. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  689. {
  690. struct pci_pbm_info *pbm = pbus->sysdata;
  691. /* Generic PCI bus probing sets these to point at
  692. * &io{port,mem}_resouce which is wrong for us.
  693. */
  694. pbus->resource[0] = &pbm->io_space;
  695. pbus->resource[1] = &pbm->mem_space;
  696. }
  697. struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
  698. {
  699. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  700. struct resource *root = NULL;
  701. if (r->flags & IORESOURCE_IO)
  702. root = &pbm->io_space;
  703. if (r->flags & IORESOURCE_MEM)
  704. root = &pbm->mem_space;
  705. return root;
  706. }
  707. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  708. {
  709. }
  710. void pcibios_align_resource(void *data, struct resource *res,
  711. resource_size_t size, resource_size_t align)
  712. {
  713. }
  714. int pcibios_enable_device(struct pci_dev *dev, int mask)
  715. {
  716. u16 cmd, oldcmd;
  717. int i;
  718. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  719. oldcmd = cmd;
  720. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  721. struct resource *res = &dev->resource[i];
  722. /* Only set up the requested stuff */
  723. if (!(mask & (1<<i)))
  724. continue;
  725. if (res->flags & IORESOURCE_IO)
  726. cmd |= PCI_COMMAND_IO;
  727. if (res->flags & IORESOURCE_MEM)
  728. cmd |= PCI_COMMAND_MEMORY;
  729. }
  730. if (cmd != oldcmd) {
  731. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  732. pci_name(dev), cmd);
  733. /* Enable the appropriate bits in the PCI command register. */
  734. pci_write_config_word(dev, PCI_COMMAND, cmd);
  735. }
  736. return 0;
  737. }
  738. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  739. struct resource *res)
  740. {
  741. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  742. struct resource zero_res, *root;
  743. zero_res.start = 0;
  744. zero_res.end = 0;
  745. zero_res.flags = res->flags;
  746. if (res->flags & IORESOURCE_IO)
  747. root = &pbm->io_space;
  748. else
  749. root = &pbm->mem_space;
  750. pci_resource_adjust(&zero_res, root);
  751. region->start = res->start - zero_res.start;
  752. region->end = res->end - zero_res.start;
  753. }
  754. EXPORT_SYMBOL(pcibios_resource_to_bus);
  755. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  756. struct pci_bus_region *region)
  757. {
  758. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  759. struct resource *root;
  760. res->start = region->start;
  761. res->end = region->end;
  762. if (res->flags & IORESOURCE_IO)
  763. root = &pbm->io_space;
  764. else
  765. root = &pbm->mem_space;
  766. pci_resource_adjust(res, root);
  767. }
  768. EXPORT_SYMBOL(pcibios_bus_to_resource);
  769. char * __devinit pcibios_setup(char *str)
  770. {
  771. return str;
  772. }
  773. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  774. /* If the user uses a host-bridge as the PCI device, he may use
  775. * this to perform a raw mmap() of the I/O or MEM space behind
  776. * that controller.
  777. *
  778. * This can be useful for execution of x86 PCI bios initialization code
  779. * on a PCI card, like the xfree86 int10 stuff does.
  780. */
  781. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  782. enum pci_mmap_state mmap_state)
  783. {
  784. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  785. unsigned long space_size, user_offset, user_size;
  786. if (mmap_state == pci_mmap_io) {
  787. space_size = (pbm->io_space.end -
  788. pbm->io_space.start) + 1;
  789. } else {
  790. space_size = (pbm->mem_space.end -
  791. pbm->mem_space.start) + 1;
  792. }
  793. /* Make sure the request is in range. */
  794. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  795. user_size = vma->vm_end - vma->vm_start;
  796. if (user_offset >= space_size ||
  797. (user_offset + user_size) > space_size)
  798. return -EINVAL;
  799. if (mmap_state == pci_mmap_io) {
  800. vma->vm_pgoff = (pbm->io_space.start +
  801. user_offset) >> PAGE_SHIFT;
  802. } else {
  803. vma->vm_pgoff = (pbm->mem_space.start +
  804. user_offset) >> PAGE_SHIFT;
  805. }
  806. return 0;
  807. }
  808. /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
  809. * to the 32-bit pci bus offset for DEV requested by the user.
  810. *
  811. * Basically, the user finds the base address for his device which he wishes
  812. * to mmap. They read the 32-bit value from the config space base register,
  813. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  814. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  815. *
  816. * Returns negative error code on failure, zero on success.
  817. */
  818. static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  819. enum pci_mmap_state mmap_state)
  820. {
  821. unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
  822. unsigned long user32 = user_offset & pci_memspace_mask;
  823. unsigned long largest_base, this_base, addr32;
  824. int i;
  825. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  826. return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
  827. /* Figure out which base address this is for. */
  828. largest_base = 0UL;
  829. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  830. struct resource *rp = &dev->resource[i];
  831. /* Active? */
  832. if (!rp->flags)
  833. continue;
  834. /* Same type? */
  835. if (i == PCI_ROM_RESOURCE) {
  836. if (mmap_state != pci_mmap_mem)
  837. continue;
  838. } else {
  839. if ((mmap_state == pci_mmap_io &&
  840. (rp->flags & IORESOURCE_IO) == 0) ||
  841. (mmap_state == pci_mmap_mem &&
  842. (rp->flags & IORESOURCE_MEM) == 0))
  843. continue;
  844. }
  845. this_base = rp->start;
  846. addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
  847. if (mmap_state == pci_mmap_io)
  848. addr32 &= 0xffffff;
  849. if (addr32 <= user32 && this_base > largest_base)
  850. largest_base = this_base;
  851. }
  852. if (largest_base == 0UL)
  853. return -EINVAL;
  854. /* Now construct the final physical address. */
  855. if (mmap_state == pci_mmap_io)
  856. vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
  857. else
  858. vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
  859. return 0;
  860. }
  861. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  862. * mapping.
  863. */
  864. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  865. enum pci_mmap_state mmap_state)
  866. {
  867. vma->vm_flags |= (VM_IO | VM_RESERVED);
  868. }
  869. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  870. * device mapping.
  871. */
  872. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  873. enum pci_mmap_state mmap_state)
  874. {
  875. /* Our io_remap_pfn_range takes care of this, do nothing. */
  876. }
  877. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  878. * for this architecture. The region in the process to map is described by vm_start
  879. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  880. * The pci device structure is provided so that architectures may make mapping
  881. * decisions on a per-device or per-bus basis.
  882. *
  883. * Returns a negative error code on failure, zero on success.
  884. */
  885. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  886. enum pci_mmap_state mmap_state,
  887. int write_combine)
  888. {
  889. int ret;
  890. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  891. if (ret < 0)
  892. return ret;
  893. __pci_mmap_set_flags(dev, vma, mmap_state);
  894. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  895. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  896. ret = io_remap_pfn_range(vma, vma->vm_start,
  897. vma->vm_pgoff,
  898. vma->vm_end - vma->vm_start,
  899. vma->vm_page_prot);
  900. if (ret)
  901. return ret;
  902. return 0;
  903. }
  904. /* Return the domain nuber for this pci bus */
  905. int pci_domain_nr(struct pci_bus *pbus)
  906. {
  907. struct pci_pbm_info *pbm = pbus->sysdata;
  908. int ret;
  909. if (pbm == NULL || pbm->parent == NULL) {
  910. ret = -ENXIO;
  911. } else {
  912. ret = pbm->index;
  913. }
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(pci_domain_nr);
  917. #ifdef CONFIG_PCI_MSI
  918. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  919. {
  920. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  921. int virt_irq;
  922. if (!pbm->setup_msi_irq)
  923. return -EINVAL;
  924. return pbm->setup_msi_irq(&virt_irq, pdev, desc);
  925. }
  926. void arch_teardown_msi_irq(unsigned int virt_irq)
  927. {
  928. struct msi_desc *entry = get_irq_msi(virt_irq);
  929. struct pci_dev *pdev = entry->dev;
  930. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  931. if (!pbm->teardown_msi_irq)
  932. return;
  933. return pbm->teardown_msi_irq(virt_irq, pdev);
  934. }
  935. #endif /* !(CONFIG_PCI_MSI) */
  936. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  937. {
  938. return pdev->dev.archdata.prom_node;
  939. }
  940. EXPORT_SYMBOL(pci_device_to_OF_node);
  941. #endif /* !(CONFIG_PCI) */