rtas_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * arch/ppc64/kernel/rtas_pci.c
  3. *
  4. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  5. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  6. *
  7. * RTAS specific routines for PCI.
  8. *
  9. * Based on code from pci.c, chrp_pci.c and pSeries_pci.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/threads.h>
  27. #include <linux/pci.h>
  28. #include <linux/string.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/irq.h>
  34. #include <asm/prom.h>
  35. #include <asm/machdep.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/iommu.h>
  38. #include <asm/rtas.h>
  39. #include "mpic.h"
  40. #include "pci.h"
  41. /* RTAS tokens */
  42. static int read_pci_config;
  43. static int write_pci_config;
  44. static int ibm_read_pci_config;
  45. static int ibm_write_pci_config;
  46. static int config_access_valid(struct pci_dn *dn, int where)
  47. {
  48. if (where < 256)
  49. return 1;
  50. if (where < 4096 && dn->pci_ext_config_space)
  51. return 1;
  52. return 0;
  53. }
  54. static int of_device_available(struct device_node * dn)
  55. {
  56. char * status;
  57. status = get_property(dn, "status", NULL);
  58. if (!status)
  59. return 1;
  60. if (!strcmp(status, "okay"))
  61. return 1;
  62. return 0;
  63. }
  64. static int rtas_read_config(struct device_node *dn, int where, int size, u32 *val)
  65. {
  66. int returnval = -1;
  67. unsigned long buid, addr;
  68. int ret;
  69. struct pci_dn *pdn;
  70. if (!dn || !dn->data)
  71. return PCIBIOS_DEVICE_NOT_FOUND;
  72. pdn = dn->data;
  73. if (!config_access_valid(pdn, where))
  74. return PCIBIOS_BAD_REGISTER_NUMBER;
  75. addr = ((where & 0xf00) << 20) | (pdn->busno << 16) |
  76. (pdn->devfn << 8) | (where & 0xff);
  77. buid = pdn->phb->buid;
  78. if (buid) {
  79. ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval,
  80. addr, buid >> 32, buid & 0xffffffff, size);
  81. } else {
  82. ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
  83. }
  84. *val = returnval;
  85. if (ret)
  86. return PCIBIOS_DEVICE_NOT_FOUND;
  87. if (returnval == EEH_IO_ERROR_VALUE(size) &&
  88. eeh_dn_check_failure (dn, NULL))
  89. return PCIBIOS_DEVICE_NOT_FOUND;
  90. return PCIBIOS_SUCCESSFUL;
  91. }
  92. static int rtas_pci_read_config(struct pci_bus *bus,
  93. unsigned int devfn,
  94. int where, int size, u32 *val)
  95. {
  96. struct device_node *busdn, *dn;
  97. if (bus->self)
  98. busdn = pci_device_to_OF_node(bus->self);
  99. else
  100. busdn = bus->sysdata; /* must be a phb */
  101. /* Search only direct children of the bus */
  102. for (dn = busdn->child; dn; dn = dn->sibling)
  103. if (dn->data && PCI_DN(dn)->devfn == devfn
  104. && of_device_available(dn))
  105. return rtas_read_config(dn, where, size, val);
  106. return PCIBIOS_DEVICE_NOT_FOUND;
  107. }
  108. int rtas_write_config(struct device_node *dn, int where, int size, u32 val)
  109. {
  110. unsigned long buid, addr;
  111. int ret;
  112. struct pci_dn *pdn;
  113. if (!dn || !dn->data)
  114. return PCIBIOS_DEVICE_NOT_FOUND;
  115. pdn = dn->data;
  116. if (!config_access_valid(pdn, where))
  117. return PCIBIOS_BAD_REGISTER_NUMBER;
  118. addr = ((where & 0xf00) << 20) | (pdn->busno << 16) |
  119. (pdn->devfn << 8) | (where & 0xff);
  120. buid = pdn->phb->buid;
  121. if (buid) {
  122. ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, buid >> 32, buid & 0xffffffff, size, (ulong) val);
  123. } else {
  124. ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
  125. }
  126. if (ret)
  127. return PCIBIOS_DEVICE_NOT_FOUND;
  128. return PCIBIOS_SUCCESSFUL;
  129. }
  130. static int rtas_pci_write_config(struct pci_bus *bus,
  131. unsigned int devfn,
  132. int where, int size, u32 val)
  133. {
  134. struct device_node *busdn, *dn;
  135. if (bus->self)
  136. busdn = pci_device_to_OF_node(bus->self);
  137. else
  138. busdn = bus->sysdata; /* must be a phb */
  139. /* Search only direct children of the bus */
  140. for (dn = busdn->child; dn; dn = dn->sibling)
  141. if (dn->data && PCI_DN(dn)->devfn == devfn
  142. && of_device_available(dn))
  143. return rtas_write_config(dn, where, size, val);
  144. return PCIBIOS_DEVICE_NOT_FOUND;
  145. }
  146. struct pci_ops rtas_pci_ops = {
  147. rtas_pci_read_config,
  148. rtas_pci_write_config
  149. };
  150. int is_python(struct device_node *dev)
  151. {
  152. char *model = (char *)get_property(dev, "model", NULL);
  153. if (model && strstr(model, "Python"))
  154. return 1;
  155. return 0;
  156. }
  157. static int get_phb_reg_prop(struct device_node *dev,
  158. unsigned int addr_size_words,
  159. struct reg_property64 *reg)
  160. {
  161. unsigned int *ui_ptr = NULL, len;
  162. /* Found a PHB, now figure out where his registers are mapped. */
  163. ui_ptr = (unsigned int *)get_property(dev, "reg", &len);
  164. if (ui_ptr == NULL)
  165. return 1;
  166. if (addr_size_words == 1) {
  167. reg->address = ((struct reg_property32 *)ui_ptr)->address;
  168. reg->size = ((struct reg_property32 *)ui_ptr)->size;
  169. } else {
  170. *reg = *((struct reg_property64 *)ui_ptr);
  171. }
  172. return 0;
  173. }
  174. static void python_countermeasures(struct device_node *dev,
  175. unsigned int addr_size_words)
  176. {
  177. struct reg_property64 reg_struct;
  178. void __iomem *chip_regs;
  179. volatile u32 val;
  180. if (get_phb_reg_prop(dev, addr_size_words, &reg_struct))
  181. return;
  182. /* Python's register file is 1 MB in size. */
  183. chip_regs = ioremap(reg_struct.address & ~(0xfffffUL), 0x100000);
  184. /*
  185. * Firmware doesn't always clear this bit which is critical
  186. * for good performance - Anton
  187. */
  188. #define PRG_CL_RESET_VALID 0x00010000
  189. val = in_be32(chip_regs + 0xf6030);
  190. if (val & PRG_CL_RESET_VALID) {
  191. printk(KERN_INFO "Python workaround: ");
  192. val &= ~PRG_CL_RESET_VALID;
  193. out_be32(chip_regs + 0xf6030, val);
  194. /*
  195. * We must read it back for changes to
  196. * take effect
  197. */
  198. val = in_be32(chip_regs + 0xf6030);
  199. printk("reg0: %x\n", val);
  200. }
  201. iounmap(chip_regs);
  202. }
  203. void __init init_pci_config_tokens (void)
  204. {
  205. read_pci_config = rtas_token("read-pci-config");
  206. write_pci_config = rtas_token("write-pci-config");
  207. ibm_read_pci_config = rtas_token("ibm,read-pci-config");
  208. ibm_write_pci_config = rtas_token("ibm,write-pci-config");
  209. }
  210. unsigned long __devinit get_phb_buid (struct device_node *phb)
  211. {
  212. int addr_cells;
  213. unsigned int *buid_vals;
  214. unsigned int len;
  215. unsigned long buid;
  216. if (ibm_read_pci_config == -1) return 0;
  217. /* PHB's will always be children of the root node,
  218. * or so it is promised by the current firmware. */
  219. if (phb->parent == NULL)
  220. return 0;
  221. if (phb->parent->parent)
  222. return 0;
  223. buid_vals = (unsigned int *) get_property(phb, "reg", &len);
  224. if (buid_vals == NULL)
  225. return 0;
  226. addr_cells = prom_n_addr_cells(phb);
  227. if (addr_cells == 1) {
  228. buid = (unsigned long) buid_vals[0];
  229. } else {
  230. buid = (((unsigned long)buid_vals[0]) << 32UL) |
  231. (((unsigned long)buid_vals[1]) & 0xffffffff);
  232. }
  233. return buid;
  234. }
  235. static int phb_set_bus_ranges(struct device_node *dev,
  236. struct pci_controller *phb)
  237. {
  238. int *bus_range;
  239. unsigned int len;
  240. bus_range = (int *) get_property(dev, "bus-range", &len);
  241. if (bus_range == NULL || len < 2 * sizeof(int)) {
  242. return 1;
  243. }
  244. phb->first_busno = bus_range[0];
  245. phb->last_busno = bus_range[1];
  246. return 0;
  247. }
  248. static int __devinit setup_phb(struct device_node *dev,
  249. struct pci_controller *phb,
  250. unsigned int addr_size_words)
  251. {
  252. pci_setup_pci_controller(phb);
  253. if (is_python(dev))
  254. python_countermeasures(dev, addr_size_words);
  255. if (phb_set_bus_ranges(dev, phb))
  256. return 1;
  257. phb->arch_data = dev;
  258. phb->ops = &rtas_pci_ops;
  259. phb->buid = get_phb_buid(dev);
  260. return 0;
  261. }
  262. static void __devinit add_linux_pci_domain(struct device_node *dev,
  263. struct pci_controller *phb,
  264. struct property *of_prop)
  265. {
  266. memset(of_prop, 0, sizeof(struct property));
  267. of_prop->name = "linux,pci-domain";
  268. of_prop->length = sizeof(phb->global_number);
  269. of_prop->value = (unsigned char *)&of_prop[1];
  270. memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
  271. prom_add_property(dev, of_prop);
  272. }
  273. static struct pci_controller * __init alloc_phb(struct device_node *dev,
  274. unsigned int addr_size_words)
  275. {
  276. struct pci_controller *phb;
  277. struct property *of_prop;
  278. phb = alloc_bootmem(sizeof(struct pci_controller));
  279. if (phb == NULL)
  280. return NULL;
  281. of_prop = alloc_bootmem(sizeof(struct property) +
  282. sizeof(phb->global_number));
  283. if (!of_prop)
  284. return NULL;
  285. if (setup_phb(dev, phb, addr_size_words))
  286. return NULL;
  287. add_linux_pci_domain(dev, phb, of_prop);
  288. return phb;
  289. }
  290. static struct pci_controller * __devinit alloc_phb_dynamic(struct device_node *dev, unsigned int addr_size_words)
  291. {
  292. struct pci_controller *phb;
  293. phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller),
  294. GFP_KERNEL);
  295. if (phb == NULL)
  296. return NULL;
  297. if (setup_phb(dev, phb, addr_size_words))
  298. return NULL;
  299. phb->is_dynamic = 1;
  300. /* TODO: linux,pci-domain? */
  301. return phb;
  302. }
  303. unsigned long __init find_and_init_phbs(void)
  304. {
  305. struct device_node *node;
  306. struct pci_controller *phb;
  307. unsigned int root_size_cells = 0;
  308. unsigned int index;
  309. unsigned int *opprop = NULL;
  310. struct device_node *root = of_find_node_by_path("/");
  311. if (ppc64_interrupt_controller == IC_OPEN_PIC) {
  312. opprop = (unsigned int *)get_property(root,
  313. "platform-open-pic", NULL);
  314. }
  315. root_size_cells = prom_n_size_cells(root);
  316. index = 0;
  317. for (node = of_get_next_child(root, NULL);
  318. node != NULL;
  319. node = of_get_next_child(root, node)) {
  320. if (node->type == NULL || strcmp(node->type, "pci") != 0)
  321. continue;
  322. phb = alloc_phb(node, root_size_cells);
  323. if (!phb)
  324. continue;
  325. pci_process_bridge_OF_ranges(phb, node);
  326. pci_setup_phb_io(phb, index == 0);
  327. #ifdef CONFIG_PPC_PSERIES
  328. if (ppc64_interrupt_controller == IC_OPEN_PIC && pSeries_mpic) {
  329. int addr = root_size_cells * (index + 2) - 1;
  330. mpic_assign_isu(pSeries_mpic, index, opprop[addr]);
  331. }
  332. #endif
  333. index++;
  334. }
  335. of_node_put(root);
  336. pci_devs_phb_init();
  337. /*
  338. * pci_probe_only and pci_assign_all_buses can be set via properties
  339. * in chosen.
  340. */
  341. if (of_chosen) {
  342. int *prop;
  343. prop = (int *)get_property(of_chosen, "linux,pci-probe-only",
  344. NULL);
  345. if (prop)
  346. pci_probe_only = *prop;
  347. prop = (int *)get_property(of_chosen,
  348. "linux,pci-assign-all-buses", NULL);
  349. if (prop)
  350. pci_assign_all_buses = *prop;
  351. }
  352. return 0;
  353. }
  354. struct pci_controller * __devinit init_phb_dynamic(struct device_node *dn)
  355. {
  356. struct device_node *root = of_find_node_by_path("/");
  357. unsigned int root_size_cells = 0;
  358. struct pci_controller *phb;
  359. struct pci_bus *bus;
  360. int primary;
  361. root_size_cells = prom_n_size_cells(root);
  362. primary = list_empty(&hose_list);
  363. phb = alloc_phb_dynamic(dn, root_size_cells);
  364. if (!phb)
  365. return NULL;
  366. pci_process_bridge_OF_ranges(phb, dn);
  367. pci_setup_phb_io_dynamic(phb, primary);
  368. of_node_put(root);
  369. pci_devs_phb_init_dynamic(phb);
  370. phb->last_busno = 0xff;
  371. bus = pci_scan_bus(phb->first_busno, phb->ops, phb->arch_data);
  372. phb->bus = bus;
  373. phb->last_busno = bus->subordinate;
  374. return phb;
  375. }
  376. EXPORT_SYMBOL(init_phb_dynamic);
  377. /* RPA-specific bits for removing PHBs */
  378. int pcibios_remove_root_bus(struct pci_controller *phb)
  379. {
  380. struct pci_bus *b = phb->bus;
  381. struct resource *res;
  382. int rc, i;
  383. res = b->resource[0];
  384. if (!res->flags) {
  385. printk(KERN_ERR "%s: no IO resource for PHB %s\n", __FUNCTION__,
  386. b->name);
  387. return 1;
  388. }
  389. rc = unmap_bus_range(b);
  390. if (rc) {
  391. printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
  392. __FUNCTION__, b->name);
  393. return 1;
  394. }
  395. if (release_resource(res)) {
  396. printk(KERN_ERR "%s: failed to release IO on bus %s\n",
  397. __FUNCTION__, b->name);
  398. return 1;
  399. }
  400. for (i = 1; i < 3; ++i) {
  401. res = b->resource[i];
  402. if (!res->flags && i == 0) {
  403. printk(KERN_ERR "%s: no MEM resource for PHB %s\n",
  404. __FUNCTION__, b->name);
  405. return 1;
  406. }
  407. if (res->flags && release_resource(res)) {
  408. printk(KERN_ERR
  409. "%s: failed to release IO %d on bus %s\n",
  410. __FUNCTION__, i, b->name);
  411. return 1;
  412. }
  413. }
  414. list_del(&phb->list_node);
  415. if (phb->is_dynamic)
  416. kfree(phb);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(pcibios_remove_root_bus);