pci.c 29 KB

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