pci.c 29 KB

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