pci.c 29 KB

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