pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  3. * Copyright (C) 2005,2007 Stephen Rothwell, IBM Corp
  4. *
  5. * iSeries specific routines for PCI.
  6. *
  7. * Based on code from pci.c and iSeries_pci.c 32bit
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #undef DEBUG
  24. #include <linux/jiffies.h>
  25. #include <linux/kernel.h>
  26. #include <linux/list.h>
  27. #include <linux/string.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/pci.h>
  31. #include <linux/of.h>
  32. #include <asm/types.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/prom.h>
  36. #include <asm/machdep.h>
  37. #include <asm/pci-bridge.h>
  38. #include <asm/iommu.h>
  39. #include <asm/abs_addr.h>
  40. #include <asm/firmware.h>
  41. #include <asm/iseries/hv_types.h>
  42. #include <asm/iseries/hv_call_xm.h>
  43. #include <asm/iseries/mf.h>
  44. #include <asm/iseries/iommu.h>
  45. #include <asm/ppc-pci.h>
  46. #include "irq.h"
  47. #include "pci.h"
  48. #include "call_pci.h"
  49. #define PCI_RETRY_MAX 3
  50. static int limit_pci_retries = 1; /* Set Retry Error on. */
  51. /*
  52. * Table defines
  53. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  54. */
  55. #define IOMM_TABLE_MAX_ENTRIES 1024
  56. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  57. #define BASE_IO_MEMORY 0xE000000000000000UL
  58. #define END_IO_MEMORY 0xEFFFFFFFFFFFFFFFUL
  59. static unsigned long max_io_memory = BASE_IO_MEMORY;
  60. static long current_iomm_table_entry;
  61. /*
  62. * Lookup Tables.
  63. */
  64. static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
  65. static u64 ds_addr_table[IOMM_TABLE_MAX_ENTRIES];
  66. static DEFINE_SPINLOCK(iomm_table_lock);
  67. /*
  68. * Generate a Direct Select Address for the Hypervisor
  69. */
  70. static inline u64 iseries_ds_addr(struct device_node *node)
  71. {
  72. struct pci_dn *pdn = PCI_DN(node);
  73. const u32 *sbp = of_get_property(node, "linux,subbus", NULL);
  74. return ((u64)pdn->busno << 48) + ((u64)(sbp ? *sbp : 0) << 40)
  75. + ((u64)0x10 << 32);
  76. }
  77. /*
  78. * Size of Bus VPD data
  79. */
  80. #define BUS_VPDSIZE 1024
  81. /*
  82. * Bus Vpd Tags
  83. */
  84. #define VPD_END_OF_AREA 0x79
  85. #define VPD_ID_STRING 0x82
  86. #define VPD_VENDOR_AREA 0x84
  87. /*
  88. * Mfg Area Tags
  89. */
  90. #define VPD_FRU_FRAME_ID 0x4649 /* "FI" */
  91. #define VPD_SLOT_MAP_FORMAT 0x4D46 /* "MF" */
  92. #define VPD_SLOT_MAP 0x534D /* "SM" */
  93. /*
  94. * Structures of the areas
  95. */
  96. struct mfg_vpd_area {
  97. u16 tag;
  98. u8 length;
  99. u8 data1;
  100. u8 data2;
  101. };
  102. #define MFG_ENTRY_SIZE 3
  103. struct slot_map {
  104. u8 agent;
  105. u8 secondary_agent;
  106. u8 phb;
  107. char card_location[3];
  108. char parms[8];
  109. char reserved[2];
  110. };
  111. #define SLOT_ENTRY_SIZE 16
  112. /*
  113. * Parse the Slot Area
  114. */
  115. static void __init iseries_parse_slot_area(struct slot_map *map, int len,
  116. HvAgentId agent, u8 *phb, char card[4])
  117. {
  118. /*
  119. * Parse Slot label until we find the one requested
  120. */
  121. while (len > 0) {
  122. if (map->agent == agent) {
  123. /*
  124. * If Phb wasn't found, grab the entry first one found.
  125. */
  126. if (*phb == 0xff)
  127. *phb = map->phb;
  128. /* Found it, extract the data. */
  129. if (map->phb == *phb) {
  130. memcpy(card, &map->card_location, 3);
  131. card[3] = 0;
  132. break;
  133. }
  134. }
  135. /* Point to the next Slot */
  136. map = (struct slot_map *)((char *)map + SLOT_ENTRY_SIZE);
  137. len -= SLOT_ENTRY_SIZE;
  138. }
  139. }
  140. /*
  141. * Parse the Mfg Area
  142. */
  143. static void __init iseries_parse_mfg_area(struct mfg_vpd_area *area, int len,
  144. HvAgentId agent, u8 *phb, u8 *frame, char card[4])
  145. {
  146. u16 slot_map_fmt = 0;
  147. /* Parse Mfg Data */
  148. while (len > 0) {
  149. int mfg_tag_len = area->length;
  150. /* Frame ID (FI 4649020310 ) */
  151. if (area->tag == VPD_FRU_FRAME_ID)
  152. *frame = area->data1;
  153. /* Slot Map Format (MF 4D46020004 ) */
  154. else if (area->tag == VPD_SLOT_MAP_FORMAT)
  155. slot_map_fmt = (area->data1 * 256)
  156. + area->data2;
  157. /* Slot Map (SM 534D90 */
  158. else if (area->tag == VPD_SLOT_MAP) {
  159. struct slot_map *slot_map;
  160. if (slot_map_fmt == 0x1004)
  161. slot_map = (struct slot_map *)((char *)area
  162. + MFG_ENTRY_SIZE + 1);
  163. else
  164. slot_map = (struct slot_map *)((char *)area
  165. + MFG_ENTRY_SIZE);
  166. iseries_parse_slot_area(slot_map, mfg_tag_len,
  167. agent, phb, card);
  168. }
  169. /*
  170. * Point to the next Mfg Area
  171. * Use defined size, sizeof give wrong answer
  172. */
  173. area = (struct mfg_vpd_area *)((char *)area + mfg_tag_len
  174. + MFG_ENTRY_SIZE);
  175. len -= (mfg_tag_len + MFG_ENTRY_SIZE);
  176. }
  177. }
  178. /*
  179. * Look for "BUS".. Data is not Null terminated.
  180. * PHBID of 0xFF indicates PHB was not found in VPD Data.
  181. */
  182. static u8 __init iseries_parse_phbid(u8 *area, int len)
  183. {
  184. while (len > 0) {
  185. if ((*area == 'B') && (*(area + 1) == 'U')
  186. && (*(area + 2) == 'S')) {
  187. area += 3;
  188. while (*area == ' ')
  189. area++;
  190. return *area & 0x0F;
  191. }
  192. area++;
  193. len--;
  194. }
  195. return 0xff;
  196. }
  197. /*
  198. * Parse out the VPD Areas
  199. */
  200. static void __init iseries_parse_vpd(u8 *data, int data_len,
  201. HvAgentId agent, u8 *frame, char card[4])
  202. {
  203. u8 phb = 0xff;
  204. while (data_len > 0) {
  205. int len;
  206. u8 tag = *data;
  207. if (tag == VPD_END_OF_AREA)
  208. break;
  209. len = *(data + 1) + (*(data + 2) * 256);
  210. data += 3;
  211. data_len -= 3;
  212. if (tag == VPD_ID_STRING)
  213. phb = iseries_parse_phbid(data, len);
  214. else if (tag == VPD_VENDOR_AREA)
  215. iseries_parse_mfg_area((struct mfg_vpd_area *)data, len,
  216. agent, &phb, frame, card);
  217. /* Point to next Area. */
  218. data += len;
  219. data_len -= len;
  220. }
  221. }
  222. static int __init iseries_get_location_code(u16 bus, HvAgentId agent,
  223. u8 *frame, char card[4])
  224. {
  225. int status = 0;
  226. int bus_vpd_len = 0;
  227. u8 *bus_vpd = kmalloc(BUS_VPDSIZE, GFP_KERNEL);
  228. if (bus_vpd == NULL) {
  229. printk("PCI: Bus VPD Buffer allocation failure.\n");
  230. return 0;
  231. }
  232. bus_vpd_len = HvCallPci_getBusVpd(bus, iseries_hv_addr(bus_vpd),
  233. BUS_VPDSIZE);
  234. if (bus_vpd_len == 0) {
  235. printk("PCI: Bus VPD Buffer zero length.\n");
  236. goto out_free;
  237. }
  238. /* printk("PCI: bus_vpd: %p, %d\n",bus_vpd, bus_vpd_len); */
  239. /* Make sure this is what I think it is */
  240. if (*bus_vpd != VPD_ID_STRING) {
  241. printk("PCI: Bus VPD Buffer missing starting tag.\n");
  242. goto out_free;
  243. }
  244. iseries_parse_vpd(bus_vpd, bus_vpd_len, agent, frame, card);
  245. status = 1;
  246. out_free:
  247. kfree(bus_vpd);
  248. return status;
  249. }
  250. /*
  251. * Prints the device information.
  252. * - Pass in pci_dev* pointer to the device.
  253. * - Pass in the device count
  254. *
  255. * Format:
  256. * PCI: Bus 0, Device 26, Vendor 0x12AE Frame 1, Card C10 Ethernet
  257. * controller
  258. */
  259. static void __init iseries_device_information(struct pci_dev *pdev,
  260. u16 bus, HvSubBusNumber subbus)
  261. {
  262. u8 frame = 0;
  263. char card[4];
  264. HvAgentId agent;
  265. agent = ISERIES_PCI_AGENTID(ISERIES_GET_DEVICE_FROM_SUBBUS(subbus),
  266. ISERIES_GET_FUNCTION_FROM_SUBBUS(subbus));
  267. if (iseries_get_location_code(bus, agent, &frame, card)) {
  268. printk(KERN_INFO "PCI: %s, Vendor %04X Frame%3d, "
  269. "Card %4s 0x%04X\n", pci_name(pdev), pdev->vendor,
  270. frame, card, (int)(pdev->class >> 8));
  271. }
  272. }
  273. /*
  274. * iomm_table_allocate_entry
  275. *
  276. * Adds pci_dev entry in address translation table
  277. *
  278. * - Allocates the number of entries required in table base on BAR
  279. * size.
  280. * - Allocates starting at BASE_IO_MEMORY and increases.
  281. * - The size is round up to be a multiple of entry size.
  282. * - CurrentIndex is incremented to keep track of the last entry.
  283. * - Builds the resource entry for allocated BARs.
  284. */
  285. static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  286. {
  287. struct resource *bar_res = &dev->resource[bar_num];
  288. long bar_size = pci_resource_len(dev, bar_num);
  289. /*
  290. * No space to allocate, quick exit, skip Allocation.
  291. */
  292. if (bar_size == 0)
  293. return;
  294. /*
  295. * Set Resource values.
  296. */
  297. spin_lock(&iomm_table_lock);
  298. bar_res->start = BASE_IO_MEMORY +
  299. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  300. bar_res->end = bar_res->start + bar_size - 1;
  301. /*
  302. * Allocate the number of table entries needed for BAR.
  303. */
  304. while (bar_size > 0 ) {
  305. iomm_table[current_iomm_table_entry] = dev->sysdata;
  306. ds_addr_table[current_iomm_table_entry] =
  307. iseries_ds_addr(dev->sysdata) | (bar_num << 24);
  308. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  309. ++current_iomm_table_entry;
  310. }
  311. max_io_memory = BASE_IO_MEMORY +
  312. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  313. spin_unlock(&iomm_table_lock);
  314. }
  315. /*
  316. * allocate_device_bars
  317. *
  318. * - Allocates ALL pci_dev BAR's and updates the resources with the
  319. * BAR value. BARS with zero length will have the resources
  320. * The HvCallPci_getBarParms is used to get the size of the BAR
  321. * space. It calls iomm_table_allocate_entry to allocate
  322. * each entry.
  323. * - Loops through The Bar resources(0 - 5) including the ROM
  324. * is resource(6).
  325. */
  326. static void __init allocate_device_bars(struct pci_dev *dev)
  327. {
  328. int bar_num;
  329. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
  330. iomm_table_allocate_entry(dev, bar_num);
  331. }
  332. /*
  333. * Log error information to system console.
  334. * Filter out the device not there errors.
  335. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  336. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  337. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  338. */
  339. static void pci_log_error(char *error, int bus, int subbus,
  340. int agent, int hv_res)
  341. {
  342. if (hv_res == 0x0302)
  343. return;
  344. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  345. error, bus, subbus, agent, hv_res);
  346. }
  347. /*
  348. * Look down the chain to find the matching Device Device
  349. */
  350. static struct device_node *find_device_node(int bus, int devfn)
  351. {
  352. struct device_node *node;
  353. for (node = NULL; (node = of_find_all_nodes(node)); ) {
  354. struct pci_dn *pdn = PCI_DN(node);
  355. if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
  356. return node;
  357. }
  358. return NULL;
  359. }
  360. /*
  361. * iSeries_pcibios_fixup_resources
  362. *
  363. * Fixes up all resources for devices
  364. */
  365. void __init iSeries_pcibios_fixup_resources(struct pci_dev *pdev)
  366. {
  367. const u32 *agent;
  368. const u32 *sub_bus;
  369. unsigned char bus = pdev->bus->number;
  370. struct device_node *node;
  371. int i;
  372. node = find_device_node(bus, pdev->devfn);
  373. pr_debug("PCI: iSeries %s, pdev %p, node %p\n",
  374. pci_name(pdev), pdev, node);
  375. if (!node) {
  376. printk("PCI: %s disabled, device tree entry not found !\n",
  377. pci_name(pdev));
  378. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  379. pdev->resource[i].flags = 0;
  380. return;
  381. }
  382. sub_bus = of_get_property(node, "linux,subbus", NULL);
  383. agent = of_get_property(node, "linux,agent-id", NULL);
  384. if (agent && sub_bus) {
  385. u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
  386. int err;
  387. err = HvCallXm_connectBusUnit(bus, *sub_bus, *agent, irq);
  388. if (err)
  389. pci_log_error("Connect Bus Unit",
  390. bus, *sub_bus, *agent, err);
  391. else {
  392. err = HvCallPci_configStore8(bus, *sub_bus,
  393. *agent, PCI_INTERRUPT_LINE, irq);
  394. if (err)
  395. pci_log_error("PciCfgStore Irq Failed!",
  396. bus, *sub_bus, *agent, err);
  397. else
  398. pdev->irq = irq;
  399. }
  400. }
  401. pdev->sysdata = node;
  402. allocate_device_bars(pdev);
  403. iseries_device_information(pdev, bus, *sub_bus);
  404. iommu_devnode_init_iSeries(pdev, node);
  405. }
  406. /*
  407. * iSeries_pci_final_fixup(void)
  408. */
  409. void __init iSeries_pci_final_fixup(void)
  410. {
  411. /* Fix up at the device node and pci_dev relationship */
  412. mf_display_src(0xC9000100);
  413. iSeries_activate_IRQs();
  414. mf_display_src(0xC9000200);
  415. }
  416. /*
  417. * Config space read and write functions.
  418. * For now at least, we look for the device node for the bus and devfn
  419. * that we are asked to access. It may be possible to translate the devfn
  420. * to a subbus and deviceid more directly.
  421. */
  422. static u64 hv_cfg_read_func[4] = {
  423. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  424. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  425. };
  426. static u64 hv_cfg_write_func[4] = {
  427. HvCallPciConfigStore8, HvCallPciConfigStore16,
  428. HvCallPciConfigStore32, HvCallPciConfigStore32
  429. };
  430. /*
  431. * Read PCI config space
  432. */
  433. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  434. int offset, int size, u32 *val)
  435. {
  436. struct device_node *node = find_device_node(bus->number, devfn);
  437. u64 fn;
  438. struct HvCallPci_LoadReturn ret;
  439. if (node == NULL)
  440. return PCIBIOS_DEVICE_NOT_FOUND;
  441. if (offset > 255) {
  442. *val = ~0;
  443. return PCIBIOS_BAD_REGISTER_NUMBER;
  444. }
  445. fn = hv_cfg_read_func[(size - 1) & 3];
  446. HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
  447. if (ret.rc != 0) {
  448. *val = ~0;
  449. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  450. }
  451. *val = ret.value;
  452. return 0;
  453. }
  454. /*
  455. * Write PCI config space
  456. */
  457. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  458. int offset, int size, u32 val)
  459. {
  460. struct device_node *node = find_device_node(bus->number, devfn);
  461. u64 fn;
  462. u64 ret;
  463. if (node == NULL)
  464. return PCIBIOS_DEVICE_NOT_FOUND;
  465. if (offset > 255)
  466. return PCIBIOS_BAD_REGISTER_NUMBER;
  467. fn = hv_cfg_write_func[(size - 1) & 3];
  468. ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
  469. if (ret != 0)
  470. return PCIBIOS_DEVICE_NOT_FOUND;
  471. return 0;
  472. }
  473. static struct pci_ops iSeries_pci_ops = {
  474. .read = iSeries_pci_read_config,
  475. .write = iSeries_pci_write_config
  476. };
  477. /*
  478. * Check Return Code
  479. * -> On Failure, print and log information.
  480. * Increment Retry Count, if exceeds max, panic partition.
  481. *
  482. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  483. * PCI: Device 23.90 ReadL Retry( 1)
  484. * PCI: Device 23.90 ReadL Retry Successful(1)
  485. */
  486. static int check_return_code(char *type, struct device_node *dn,
  487. int *retry, u64 ret)
  488. {
  489. if (ret != 0) {
  490. struct pci_dn *pdn = PCI_DN(dn);
  491. (*retry)++;
  492. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  493. type, pdn->busno, pdn->devfn,
  494. *retry, (int)ret);
  495. /*
  496. * Bump the retry and check for retry count exceeded.
  497. * If, Exceeded, panic the system.
  498. */
  499. if (((*retry) > PCI_RETRY_MAX) &&
  500. (limit_pci_retries > 0)) {
  501. mf_display_src(0xB6000103);
  502. panic_timeout = 0;
  503. panic("PCI: Hardware I/O Error, SRC B6000103, "
  504. "Automatic Reboot Disabled.\n");
  505. }
  506. return -1; /* Retry Try */
  507. }
  508. return 0;
  509. }
  510. /*
  511. * Translate the I/O Address into a device node, bar, and bar offset.
  512. * Note: Make sure the passed variable end up on the stack to avoid
  513. * the exposure of being device global.
  514. */
  515. static inline struct device_node *xlate_iomm_address(
  516. const volatile void __iomem *addr,
  517. u64 *dsaptr, u64 *bar_offset, const char *func)
  518. {
  519. unsigned long orig_addr;
  520. unsigned long base_addr;
  521. unsigned long ind;
  522. struct device_node *dn;
  523. orig_addr = (unsigned long __force)addr;
  524. if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
  525. static unsigned long last_jiffies;
  526. static int num_printed;
  527. if (time_after(jiffies, last_jiffies + 60 * HZ)) {
  528. last_jiffies = jiffies;
  529. num_printed = 0;
  530. }
  531. if (num_printed++ < 10)
  532. printk(KERN_ERR
  533. "iSeries_%s: invalid access at IO address %p\n",
  534. func, addr);
  535. return NULL;
  536. }
  537. base_addr = orig_addr - BASE_IO_MEMORY;
  538. ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
  539. dn = iomm_table[ind];
  540. if (dn != NULL) {
  541. *dsaptr = ds_addr_table[ind];
  542. *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
  543. } else
  544. panic("PCI: Invalid PCI IO address detected!\n");
  545. return dn;
  546. }
  547. /*
  548. * Read MM I/O Instructions for the iSeries
  549. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  550. * else, data is returned in Big Endian format.
  551. */
  552. static u8 iseries_readb(const volatile void __iomem *addr)
  553. {
  554. u64 bar_offset;
  555. u64 dsa;
  556. int retry = 0;
  557. struct HvCallPci_LoadReturn ret;
  558. struct device_node *dn =
  559. xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
  560. if (dn == NULL)
  561. return 0xff;
  562. do {
  563. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
  564. } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
  565. return ret.value;
  566. }
  567. static u16 iseries_readw_be(const volatile void __iomem *addr)
  568. {
  569. u64 bar_offset;
  570. u64 dsa;
  571. int retry = 0;
  572. struct HvCallPci_LoadReturn ret;
  573. struct device_node *dn =
  574. xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
  575. if (dn == NULL)
  576. return 0xffff;
  577. do {
  578. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  579. bar_offset, 0);
  580. } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
  581. return ret.value;
  582. }
  583. static u32 iseries_readl_be(const volatile void __iomem *addr)
  584. {
  585. u64 bar_offset;
  586. u64 dsa;
  587. int retry = 0;
  588. struct HvCallPci_LoadReturn ret;
  589. struct device_node *dn =
  590. xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
  591. if (dn == NULL)
  592. return 0xffffffff;
  593. do {
  594. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  595. bar_offset, 0);
  596. } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
  597. return ret.value;
  598. }
  599. /*
  600. * Write MM I/O Instructions for the iSeries
  601. *
  602. */
  603. static void iseries_writeb(u8 data, volatile void __iomem *addr)
  604. {
  605. u64 bar_offset;
  606. u64 dsa;
  607. int retry = 0;
  608. u64 rc;
  609. struct device_node *dn =
  610. xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
  611. if (dn == NULL)
  612. return;
  613. do {
  614. rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
  615. } while (check_return_code("WWB", dn, &retry, rc) != 0);
  616. }
  617. static void iseries_writew_be(u16 data, volatile void __iomem *addr)
  618. {
  619. u64 bar_offset;
  620. u64 dsa;
  621. int retry = 0;
  622. u64 rc;
  623. struct device_node *dn =
  624. xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
  625. if (dn == NULL)
  626. return;
  627. do {
  628. rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
  629. } while (check_return_code("WWW", dn, &retry, rc) != 0);
  630. }
  631. static void iseries_writel_be(u32 data, volatile void __iomem *addr)
  632. {
  633. u64 bar_offset;
  634. u64 dsa;
  635. int retry = 0;
  636. u64 rc;
  637. struct device_node *dn =
  638. xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
  639. if (dn == NULL)
  640. return;
  641. do {
  642. rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
  643. } while (check_return_code("WWL", dn, &retry, rc) != 0);
  644. }
  645. static u16 iseries_readw(const volatile void __iomem *addr)
  646. {
  647. return le16_to_cpu(iseries_readw_be(addr));
  648. }
  649. static u32 iseries_readl(const volatile void __iomem *addr)
  650. {
  651. return le32_to_cpu(iseries_readl_be(addr));
  652. }
  653. static void iseries_writew(u16 data, volatile void __iomem *addr)
  654. {
  655. iseries_writew_be(cpu_to_le16(data), addr);
  656. }
  657. static void iseries_writel(u32 data, volatile void __iomem *addr)
  658. {
  659. iseries_writel(cpu_to_le32(data), addr);
  660. }
  661. static void iseries_readsb(const volatile void __iomem *addr, void *buf,
  662. unsigned long count)
  663. {
  664. u8 *dst = buf;
  665. while(count-- > 0)
  666. *(dst++) = iseries_readb(addr);
  667. }
  668. static void iseries_readsw(const volatile void __iomem *addr, void *buf,
  669. unsigned long count)
  670. {
  671. u16 *dst = buf;
  672. while(count-- > 0)
  673. *(dst++) = iseries_readw_be(addr);
  674. }
  675. static void iseries_readsl(const volatile void __iomem *addr, void *buf,
  676. unsigned long count)
  677. {
  678. u32 *dst = buf;
  679. while(count-- > 0)
  680. *(dst++) = iseries_readl_be(addr);
  681. }
  682. static void iseries_writesb(volatile void __iomem *addr, const void *buf,
  683. unsigned long count)
  684. {
  685. const u8 *src = buf;
  686. while(count-- > 0)
  687. iseries_writeb(*(src++), addr);
  688. }
  689. static void iseries_writesw(volatile void __iomem *addr, const void *buf,
  690. unsigned long count)
  691. {
  692. const u16 *src = buf;
  693. while(count-- > 0)
  694. iseries_writew_be(*(src++), addr);
  695. }
  696. static void iseries_writesl(volatile void __iomem *addr, const void *buf,
  697. unsigned long count)
  698. {
  699. const u32 *src = buf;
  700. while(count-- > 0)
  701. iseries_writel_be(*(src++), addr);
  702. }
  703. static void iseries_memset_io(volatile void __iomem *addr, int c,
  704. unsigned long n)
  705. {
  706. volatile char __iomem *d = addr;
  707. while (n-- > 0)
  708. iseries_writeb(c, d++);
  709. }
  710. static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
  711. unsigned long n)
  712. {
  713. char *d = dest;
  714. const volatile char __iomem *s = src;
  715. while (n-- > 0)
  716. *d++ = iseries_readb(s++);
  717. }
  718. static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
  719. unsigned long n)
  720. {
  721. const char *s = src;
  722. volatile char __iomem *d = dest;
  723. while (n-- > 0)
  724. iseries_writeb(*s++, d++);
  725. }
  726. /* We only set MMIO ops. The default PIO ops will be default
  727. * to the MMIO ops + pci_io_base which is 0 on iSeries as
  728. * expected so both should work.
  729. *
  730. * Note that we don't implement the readq/writeq versions as
  731. * I don't know of an HV call for doing so. Thus, the default
  732. * operation will be used instead, which will fault a the value
  733. * return by iSeries for MMIO addresses always hits a non mapped
  734. * area. This is as good as the BUG() we used to have there.
  735. */
  736. static struct ppc_pci_io __initdata iseries_pci_io = {
  737. .readb = iseries_readb,
  738. .readw = iseries_readw,
  739. .readl = iseries_readl,
  740. .readw_be = iseries_readw_be,
  741. .readl_be = iseries_readl_be,
  742. .writeb = iseries_writeb,
  743. .writew = iseries_writew,
  744. .writel = iseries_writel,
  745. .writew_be = iseries_writew_be,
  746. .writel_be = iseries_writel_be,
  747. .readsb = iseries_readsb,
  748. .readsw = iseries_readsw,
  749. .readsl = iseries_readsl,
  750. .writesb = iseries_writesb,
  751. .writesw = iseries_writesw,
  752. .writesl = iseries_writesl,
  753. .memset_io = iseries_memset_io,
  754. .memcpy_fromio = iseries_memcpy_fromio,
  755. .memcpy_toio = iseries_memcpy_toio,
  756. };
  757. /*
  758. * iSeries_pcibios_init
  759. *
  760. * Description:
  761. * This function checks for all possible system PCI host bridges that connect
  762. * PCI buses. The system hypervisor is queried as to the guest partition
  763. * ownership status. A pci_controller is built for any bus which is partially
  764. * owned or fully owned by this guest partition.
  765. */
  766. void __init iSeries_pcibios_init(void)
  767. {
  768. struct pci_controller *phb;
  769. struct device_node *root = of_find_node_by_path("/");
  770. struct device_node *node = NULL;
  771. /* Install IO hooks */
  772. ppc_pci_io = iseries_pci_io;
  773. pci_probe_only = 1;
  774. /* iSeries has no IO space in the common sense, it needs to set
  775. * the IO base to 0
  776. */
  777. pci_io_base = 0;
  778. if (root == NULL) {
  779. printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
  780. "of device tree\n");
  781. return;
  782. }
  783. while ((node = of_get_next_child(root, node)) != NULL) {
  784. HvBusNumber bus;
  785. const u32 *busp;
  786. if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
  787. continue;
  788. busp = of_get_property(node, "bus-range", NULL);
  789. if (busp == NULL)
  790. continue;
  791. bus = *busp;
  792. printk("bus %d appears to exist\n", bus);
  793. phb = pcibios_alloc_controller(node);
  794. if (phb == NULL)
  795. continue;
  796. /* All legacy iSeries PHBs are in domain zero */
  797. phb->global_number = 0;
  798. phb->first_busno = bus;
  799. phb->last_busno = bus;
  800. phb->ops = &iSeries_pci_ops;
  801. phb->io_base_virt = (void __iomem *)_IO_BASE;
  802. phb->io_resource.flags = IORESOURCE_IO;
  803. phb->io_resource.start = BASE_IO_MEMORY;
  804. phb->io_resource.end = END_IO_MEMORY;
  805. phb->io_resource.name = "iSeries PCI IO";
  806. phb->mem_resources[0].flags = IORESOURCE_MEM;
  807. phb->mem_resources[0].start = BASE_IO_MEMORY;
  808. phb->mem_resources[0].end = END_IO_MEMORY;
  809. phb->mem_resources[0].name = "Series PCI MEM";
  810. }
  811. of_node_put(root);
  812. pci_devs_phb_init();
  813. }