pci.c 28 KB

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