iSeries_pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * iSeries_pci.c
  3. *
  4. * Copyright (C) 2001 Allan Trautman, IBM Corporation
  5. *
  6. * iSeries specific routines for PCI.
  7. *
  8. * Based on code from pci.c and iSeries_pci.c 32bit
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/ide.h>
  30. #include <linux/pci.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <asm/prom.h>
  34. #include <asm/machdep.h>
  35. #include <asm/pci-bridge.h>
  36. #include <asm/ppcdebug.h>
  37. #include <asm/iommu.h>
  38. #include <asm/iSeries/HvCallPci.h>
  39. #include <asm/iSeries/HvCallXm.h>
  40. #include <asm/iSeries/iSeries_irq.h>
  41. #include <asm/iSeries/iSeries_pci.h>
  42. #include <asm/iSeries/mf.h>
  43. #include "pci.h"
  44. extern unsigned long io_page_mask;
  45. /*
  46. * Forward declares of prototypes.
  47. */
  48. static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn);
  49. static void scan_PHB_slots(struct pci_controller *Phb);
  50. static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
  51. static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
  52. LIST_HEAD(iSeries_Global_Device_List);
  53. static int DeviceCount;
  54. /* Counters and control flags. */
  55. static long Pci_Io_Read_Count;
  56. static long Pci_Io_Write_Count;
  57. #if 0
  58. static long Pci_Cfg_Read_Count;
  59. static long Pci_Cfg_Write_Count;
  60. #endif
  61. static long Pci_Error_Count;
  62. static int Pci_Retry_Max = 3; /* Only retry 3 times */
  63. static int Pci_Error_Flag = 1; /* Set Retry Error on. */
  64. static struct pci_ops iSeries_pci_ops;
  65. /*
  66. * Table defines
  67. * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
  68. */
  69. #define IOMM_TABLE_MAX_ENTRIES 1024
  70. #define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
  71. #define BASE_IO_MEMORY 0xE000000000000000UL
  72. static unsigned long max_io_memory = 0xE000000000000000UL;
  73. static long current_iomm_table_entry;
  74. /*
  75. * Lookup Tables.
  76. */
  77. static struct iSeries_Device_Node **iomm_table;
  78. static u8 *iobar_table;
  79. /*
  80. * Static and Global variables
  81. */
  82. static char *pci_io_text = "iSeries PCI I/O";
  83. static DEFINE_SPINLOCK(iomm_table_lock);
  84. /*
  85. * iomm_table_initialize
  86. *
  87. * Allocates and initalizes the Address Translation Table and Bar
  88. * Tables to get them ready for use. Must be called before any
  89. * I/O space is handed out to the device BARs.
  90. */
  91. static void iomm_table_initialize(void)
  92. {
  93. spin_lock(&iomm_table_lock);
  94. iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
  95. GFP_KERNEL);
  96. iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
  97. GFP_KERNEL);
  98. spin_unlock(&iomm_table_lock);
  99. if ((iomm_table == NULL) || (iobar_table == NULL))
  100. panic("PCI: I/O tables allocation failed.\n");
  101. }
  102. /*
  103. * iomm_table_allocate_entry
  104. *
  105. * Adds pci_dev entry in address translation table
  106. *
  107. * - Allocates the number of entries required in table base on BAR
  108. * size.
  109. * - Allocates starting at BASE_IO_MEMORY and increases.
  110. * - The size is round up to be a multiple of entry size.
  111. * - CurrentIndex is incremented to keep track of the last entry.
  112. * - Builds the resource entry for allocated BARs.
  113. */
  114. static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
  115. {
  116. struct resource *bar_res = &dev->resource[bar_num];
  117. long bar_size = pci_resource_len(dev, bar_num);
  118. /*
  119. * No space to allocate, quick exit, skip Allocation.
  120. */
  121. if (bar_size == 0)
  122. return;
  123. /*
  124. * Set Resource values.
  125. */
  126. spin_lock(&iomm_table_lock);
  127. bar_res->name = pci_io_text;
  128. bar_res->start =
  129. IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
  130. bar_res->start += BASE_IO_MEMORY;
  131. bar_res->end = bar_res->start + bar_size - 1;
  132. /*
  133. * Allocate the number of table entries needed for BAR.
  134. */
  135. while (bar_size > 0 ) {
  136. iomm_table[current_iomm_table_entry] = dev->sysdata;
  137. iobar_table[current_iomm_table_entry] = bar_num;
  138. bar_size -= IOMM_TABLE_ENTRY_SIZE;
  139. ++current_iomm_table_entry;
  140. }
  141. max_io_memory = BASE_IO_MEMORY +
  142. (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
  143. spin_unlock(&iomm_table_lock);
  144. }
  145. /*
  146. * allocate_device_bars
  147. *
  148. * - Allocates ALL pci_dev BAR's and updates the resources with the
  149. * BAR value. BARS with zero length will have the resources
  150. * The HvCallPci_getBarParms is used to get the size of the BAR
  151. * space. It calls iomm_table_allocate_entry to allocate
  152. * each entry.
  153. * - Loops through The Bar resources(0 - 5) including the ROM
  154. * is resource(6).
  155. */
  156. static void allocate_device_bars(struct pci_dev *dev)
  157. {
  158. struct resource *bar_res;
  159. int bar_num;
  160. for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
  161. bar_res = &dev->resource[bar_num];
  162. iomm_table_allocate_entry(dev, bar_num);
  163. }
  164. }
  165. /*
  166. * Log error information to system console.
  167. * Filter out the device not there errors.
  168. * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
  169. * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
  170. * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
  171. */
  172. static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
  173. int AgentId, int HvRc)
  174. {
  175. if (HvRc == 0x0302)
  176. return;
  177. printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
  178. Error_Text, Bus, SubBus, AgentId, HvRc);
  179. }
  180. /*
  181. * build_device_node(u16 Bus, int SubBus, u8 DevFn)
  182. */
  183. static struct iSeries_Device_Node *build_device_node(HvBusNumber Bus,
  184. HvSubBusNumber SubBus, int AgentId, int Function)
  185. {
  186. struct iSeries_Device_Node *node;
  187. PPCDBG(PPCDBG_BUSWALK,
  188. "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
  189. Bus, SubBus, AgentId, Function);
  190. node = kmalloc(sizeof(struct iSeries_Device_Node), GFP_KERNEL);
  191. if (node == NULL)
  192. return NULL;
  193. memset(node, 0, sizeof(struct iSeries_Device_Node));
  194. list_add_tail(&node->Device_List, &iSeries_Global_Device_List);
  195. #if 0
  196. node->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
  197. #endif
  198. node->DsaAddr.DsaAddr = 0;
  199. node->DsaAddr.Dsa.busNumber = Bus;
  200. node->DsaAddr.Dsa.subBusNumber = SubBus;
  201. node->DsaAddr.Dsa.deviceId = 0x10;
  202. node->AgentId = AgentId;
  203. node->DevFn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
  204. node->IoRetry = 0;
  205. iSeries_Get_Location_Code(node);
  206. return node;
  207. }
  208. /*
  209. * unsigned long __init find_and_init_phbs(void)
  210. *
  211. * Description:
  212. * This function checks for all possible system PCI host bridges that connect
  213. * PCI buses. The system hypervisor is queried as to the guest partition
  214. * ownership status. A pci_controller is built for any bus which is partially
  215. * owned or fully owned by this guest partition.
  216. */
  217. unsigned long __init find_and_init_phbs(void)
  218. {
  219. struct pci_controller *phb;
  220. HvBusNumber bus;
  221. PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
  222. /* Check all possible buses. */
  223. for (bus = 0; bus < 256; bus++) {
  224. int ret = HvCallXm_testBus(bus);
  225. if (ret == 0) {
  226. printk("bus %d appears to exist\n", bus);
  227. phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  228. if (phb == NULL)
  229. return -ENOMEM;
  230. pci_setup_pci_controller(phb);
  231. phb->pci_mem_offset = phb->local_number = bus;
  232. phb->first_busno = bus;
  233. phb->last_busno = bus;
  234. phb->ops = &iSeries_pci_ops;
  235. PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
  236. phb, bus);
  237. /* Find and connect the devices. */
  238. scan_PHB_slots(phb);
  239. }
  240. /*
  241. * Check for Unexpected Return code, a clue that something
  242. * has gone wrong.
  243. */
  244. else if (ret != 0x0301)
  245. printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
  246. bus, ret);
  247. }
  248. return 0;
  249. }
  250. /*
  251. * iSeries_pcibios_init
  252. *
  253. * Chance to initialize and structures or variable before PCI Bus walk.
  254. */
  255. void iSeries_pcibios_init(void)
  256. {
  257. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n");
  258. iomm_table_initialize();
  259. find_and_init_phbs();
  260. io_page_mask = -1;
  261. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n");
  262. }
  263. /*
  264. * iSeries_pci_final_fixup(void)
  265. */
  266. void __init iSeries_pci_final_fixup(void)
  267. {
  268. struct pci_dev *pdev = NULL;
  269. struct iSeries_Device_Node *node;
  270. char Buffer[256];
  271. int DeviceCount = 0;
  272. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n");
  273. /* Fix up at the device node and pci_dev relationship */
  274. mf_display_src(0xC9000100);
  275. printk("pcibios_final_fixup\n");
  276. for_each_pci_dev(pdev) {
  277. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  278. printk("pci dev %p (%x.%x), node %p\n", pdev,
  279. pdev->bus->number, pdev->devfn, node);
  280. if (node != NULL) {
  281. ++DeviceCount;
  282. pdev->sysdata = (void *)node;
  283. node->PciDev = pdev;
  284. PPCDBG(PPCDBG_BUSWALK,
  285. "pdev 0x%p <==> DevNode 0x%p\n",
  286. pdev, node);
  287. allocate_device_bars(pdev);
  288. iSeries_Device_Information(pdev, Buffer,
  289. sizeof(Buffer));
  290. printk("%d. %s\n", DeviceCount, Buffer);
  291. iommu_devnode_init_iSeries(node);
  292. } else
  293. printk("PCI: Device Tree not found for 0x%016lX\n",
  294. (unsigned long)pdev);
  295. pdev->irq = node->Irq;
  296. }
  297. iSeries_activate_IRQs();
  298. mf_display_src(0xC9000200);
  299. }
  300. void pcibios_fixup_bus(struct pci_bus *PciBus)
  301. {
  302. PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
  303. PciBus->number);
  304. }
  305. void pcibios_fixup_resources(struct pci_dev *pdev)
  306. {
  307. PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
  308. }
  309. /*
  310. * Loop through each node function to find usable EADs bridges.
  311. */
  312. static void scan_PHB_slots(struct pci_controller *Phb)
  313. {
  314. struct HvCallPci_DeviceInfo *DevInfo;
  315. HvBusNumber bus = Phb->local_number; /* System Bus */
  316. const HvSubBusNumber SubBus = 0; /* EADs is always 0. */
  317. int HvRc = 0;
  318. int IdSel;
  319. const int MaxAgents = 8;
  320. DevInfo = (struct HvCallPci_DeviceInfo*)
  321. kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
  322. if (DevInfo == NULL)
  323. return;
  324. /*
  325. * Probe for EADs Bridges
  326. */
  327. for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
  328. HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
  329. ISERIES_HV_ADDR(DevInfo),
  330. sizeof(struct HvCallPci_DeviceInfo));
  331. if (HvRc == 0) {
  332. if (DevInfo->deviceType == HvCallPci_NodeDevice)
  333. scan_EADS_bridge(bus, SubBus, IdSel);
  334. else
  335. printk("PCI: Invalid System Configuration(0x%02X)"
  336. " for bus 0x%02x id 0x%02x.\n",
  337. DevInfo->deviceType, bus, IdSel);
  338. }
  339. else
  340. pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
  341. }
  342. kfree(DevInfo);
  343. }
  344. static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
  345. int IdSel)
  346. {
  347. struct HvCallPci_BridgeInfo *BridgeInfo;
  348. HvAgentId AgentId;
  349. int Function;
  350. int HvRc;
  351. BridgeInfo = (struct HvCallPci_BridgeInfo *)
  352. kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
  353. if (BridgeInfo == NULL)
  354. return;
  355. /* Note: hvSubBus and irq is always be 0 at this level! */
  356. for (Function = 0; Function < 8; ++Function) {
  357. AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  358. HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
  359. if (HvRc == 0) {
  360. printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
  361. bus, IdSel, Function, AgentId);
  362. /* Connect EADs: 0x18.00.12 = 0x00 */
  363. PPCDBG(PPCDBG_BUSWALK,
  364. "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
  365. bus, SubBus, AgentId);
  366. HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
  367. ISERIES_HV_ADDR(BridgeInfo),
  368. sizeof(struct HvCallPci_BridgeInfo));
  369. if (HvRc == 0) {
  370. printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
  371. BridgeInfo->busUnitInfo.deviceType,
  372. BridgeInfo->subBusNumber,
  373. BridgeInfo->maxAgents,
  374. BridgeInfo->maxSubBusNumber,
  375. BridgeInfo->logicalSlotNumber);
  376. PPCDBG(PPCDBG_BUSWALK,
  377. "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
  378. BridgeInfo->busUnitInfo.deviceType,
  379. BridgeInfo->subBusNumber,
  380. BridgeInfo->maxAgents,
  381. BridgeInfo->maxSubBusNumber,
  382. BridgeInfo->logicalSlotNumber);
  383. if (BridgeInfo->busUnitInfo.deviceType ==
  384. HvCallPci_BridgeDevice) {
  385. /* Scan_Bridge_Slot...: 0x18.00.12 */
  386. scan_bridge_slot(bus, BridgeInfo);
  387. } else
  388. printk("PCI: Invalid Bridge Configuration(0x%02X)",
  389. BridgeInfo->busUnitInfo.deviceType);
  390. }
  391. } else if (HvRc != 0x000B)
  392. pci_Log_Error("EADs Connect",
  393. bus, SubBus, AgentId, HvRc);
  394. }
  395. kfree(BridgeInfo);
  396. }
  397. /*
  398. * This assumes that the node slot is always on the primary bus!
  399. */
  400. static int scan_bridge_slot(HvBusNumber Bus,
  401. struct HvCallPci_BridgeInfo *BridgeInfo)
  402. {
  403. struct iSeries_Device_Node *node;
  404. HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
  405. u16 VendorId = 0;
  406. int HvRc = 0;
  407. u8 Irq = 0;
  408. int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
  409. int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
  410. HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
  411. /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
  412. Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
  413. PPCDBG(PPCDBG_BUSWALK,
  414. "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
  415. Bus, 0, EADsIdSel, Irq);
  416. /*
  417. * Connect all functions of any device found.
  418. */
  419. for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
  420. for (Function = 0; Function < 8; ++Function) {
  421. HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
  422. HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
  423. AgentId, Irq);
  424. if (HvRc != 0) {
  425. pci_Log_Error("Connect Bus Unit",
  426. Bus, SubBus, AgentId, HvRc);
  427. continue;
  428. }
  429. HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
  430. PCI_VENDOR_ID, &VendorId);
  431. if (HvRc != 0) {
  432. pci_Log_Error("Read Vendor",
  433. Bus, SubBus, AgentId, HvRc);
  434. continue;
  435. }
  436. printk("read vendor ID: %x\n", VendorId);
  437. /* FoundDevice: 0x18.28.10 = 0x12AE */
  438. PPCDBG(PPCDBG_BUSWALK,
  439. "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
  440. Bus, SubBus, AgentId, VendorId, Irq);
  441. HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
  442. PCI_INTERRUPT_LINE, Irq);
  443. if (HvRc != 0)
  444. pci_Log_Error("PciCfgStore Irq Failed!",
  445. Bus, SubBus, AgentId, HvRc);
  446. ++DeviceCount;
  447. node = build_device_node(Bus, SubBus, EADsIdSel, Function);
  448. node->Irq = Irq;
  449. node->LogicalSlot = BridgeInfo->logicalSlotNumber;
  450. } /* for (Function = 0; Function < 8; ++Function) */
  451. } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
  452. return HvRc;
  453. }
  454. /*
  455. * I/0 Memory copy MUST use mmio commands on iSeries
  456. * To do; For performance, include the hv call directly
  457. */
  458. void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
  459. {
  460. u8 ByteValue = c;
  461. long NumberOfBytes = Count;
  462. while (NumberOfBytes > 0) {
  463. iSeries_Write_Byte(ByteValue, dest++);
  464. -- NumberOfBytes;
  465. }
  466. }
  467. EXPORT_SYMBOL(iSeries_memset_io);
  468. void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
  469. {
  470. char *src = source;
  471. long NumberOfBytes = count;
  472. while (NumberOfBytes > 0) {
  473. iSeries_Write_Byte(*src++, dest++);
  474. -- NumberOfBytes;
  475. }
  476. }
  477. EXPORT_SYMBOL(iSeries_memcpy_toio);
  478. void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
  479. {
  480. char *dst = dest;
  481. long NumberOfBytes = count;
  482. while (NumberOfBytes > 0) {
  483. *dst++ = iSeries_Read_Byte(src++);
  484. -- NumberOfBytes;
  485. }
  486. }
  487. EXPORT_SYMBOL(iSeries_memcpy_fromio);
  488. /*
  489. * Look down the chain to find the matching Device Device
  490. */
  491. static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn)
  492. {
  493. struct list_head *pos;
  494. list_for_each(pos, &iSeries_Global_Device_List) {
  495. struct iSeries_Device_Node *node =
  496. list_entry(pos, struct iSeries_Device_Node, Device_List);
  497. if ((bus == ISERIES_BUS(node)) && (devfn == node->DevFn))
  498. return node;
  499. }
  500. return NULL;
  501. }
  502. #if 0
  503. /*
  504. * Returns the device node for the passed pci_dev
  505. * Sanity Check Node PciDev to passed pci_dev
  506. * If none is found, returns a NULL which the client must handle.
  507. */
  508. static struct iSeries_Device_Node *get_Device_Node(struct pci_dev *pdev)
  509. {
  510. struct iSeries_Device_Node *node;
  511. node = pdev->sysdata;
  512. if (node == NULL || node->PciDev != pdev)
  513. node = find_Device_Node(pdev->bus->number, pdev->devfn);
  514. return node;
  515. }
  516. #endif
  517. /*
  518. * Config space read and write functions.
  519. * For now at least, we look for the device node for the bus and devfn
  520. * that we are asked to access. It may be possible to translate the devfn
  521. * to a subbus and deviceid more directly.
  522. */
  523. static u64 hv_cfg_read_func[4] = {
  524. HvCallPciConfigLoad8, HvCallPciConfigLoad16,
  525. HvCallPciConfigLoad32, HvCallPciConfigLoad32
  526. };
  527. static u64 hv_cfg_write_func[4] = {
  528. HvCallPciConfigStore8, HvCallPciConfigStore16,
  529. HvCallPciConfigStore32, HvCallPciConfigStore32
  530. };
  531. /*
  532. * Read PCI config space
  533. */
  534. static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  535. int offset, int size, u32 *val)
  536. {
  537. struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
  538. u64 fn;
  539. struct HvCallPci_LoadReturn ret;
  540. if (node == NULL)
  541. return PCIBIOS_DEVICE_NOT_FOUND;
  542. if (offset > 255) {
  543. *val = ~0;
  544. return PCIBIOS_BAD_REGISTER_NUMBER;
  545. }
  546. fn = hv_cfg_read_func[(size - 1) & 3];
  547. HvCall3Ret16(fn, &ret, node->DsaAddr.DsaAddr, offset, 0);
  548. if (ret.rc != 0) {
  549. *val = ~0;
  550. return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
  551. }
  552. *val = ret.value;
  553. return 0;
  554. }
  555. /*
  556. * Write PCI config space
  557. */
  558. static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  559. int offset, int size, u32 val)
  560. {
  561. struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
  562. u64 fn;
  563. u64 ret;
  564. if (node == NULL)
  565. return PCIBIOS_DEVICE_NOT_FOUND;
  566. if (offset > 255)
  567. return PCIBIOS_BAD_REGISTER_NUMBER;
  568. fn = hv_cfg_write_func[(size - 1) & 3];
  569. ret = HvCall4(fn, node->DsaAddr.DsaAddr, offset, val, 0);
  570. if (ret != 0)
  571. return PCIBIOS_DEVICE_NOT_FOUND;
  572. return 0;
  573. }
  574. static struct pci_ops iSeries_pci_ops = {
  575. .read = iSeries_pci_read_config,
  576. .write = iSeries_pci_write_config
  577. };
  578. /*
  579. * Check Return Code
  580. * -> On Failure, print and log information.
  581. * Increment Retry Count, if exceeds max, panic partition.
  582. * -> If in retry, print and log success
  583. *
  584. * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
  585. * PCI: Device 23.90 ReadL Retry( 1)
  586. * PCI: Device 23.90 ReadL Retry Successful(1)
  587. */
  588. static int CheckReturnCode(char *TextHdr, struct iSeries_Device_Node *DevNode,
  589. u64 ret)
  590. {
  591. if (ret != 0) {
  592. ++Pci_Error_Count;
  593. ++DevNode->IoRetry;
  594. printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
  595. TextHdr, DevNode->DsaAddr.Dsa.busNumber, DevNode->DevFn,
  596. DevNode->IoRetry, (int)ret);
  597. /*
  598. * Bump the retry and check for retry count exceeded.
  599. * If, Exceeded, panic the system.
  600. */
  601. if ((DevNode->IoRetry > Pci_Retry_Max) &&
  602. (Pci_Error_Flag > 0)) {
  603. mf_display_src(0xB6000103);
  604. panic_timeout = 0;
  605. panic("PCI: Hardware I/O Error, SRC B6000103, "
  606. "Automatic Reboot Disabled.\n");
  607. }
  608. return -1; /* Retry Try */
  609. }
  610. /* If retry was in progress, log success and rest retry count */
  611. if (DevNode->IoRetry > 0)
  612. DevNode->IoRetry = 0;
  613. return 0;
  614. }
  615. /*
  616. * Translate the I/O Address into a device node, bar, and bar offset.
  617. * Note: Make sure the passed variable end up on the stack to avoid
  618. * the exposure of being device global.
  619. */
  620. static inline struct iSeries_Device_Node *xlate_iomm_address(
  621. const volatile void __iomem *IoAddress,
  622. u64 *dsaptr, u64 *BarOffsetPtr)
  623. {
  624. unsigned long OrigIoAddr;
  625. unsigned long BaseIoAddr;
  626. unsigned long TableIndex;
  627. struct iSeries_Device_Node *DevNode;
  628. OrigIoAddr = (unsigned long __force)IoAddress;
  629. if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
  630. return NULL;
  631. BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
  632. TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
  633. DevNode = iomm_table[TableIndex];
  634. if (DevNode != NULL) {
  635. int barnum = iobar_table[TableIndex];
  636. *dsaptr = DevNode->DsaAddr.DsaAddr | (barnum << 24);
  637. *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
  638. } else
  639. panic("PCI: Invalid PCI IoAddress detected!\n");
  640. return DevNode;
  641. }
  642. /*
  643. * Read MM I/O Instructions for the iSeries
  644. * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
  645. * else, data is returned in big Endian format.
  646. *
  647. * iSeries_Read_Byte = Read Byte ( 8 bit)
  648. * iSeries_Read_Word = Read Word (16 bit)
  649. * iSeries_Read_Long = Read Long (32 bit)
  650. */
  651. u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
  652. {
  653. u64 BarOffset;
  654. u64 dsa;
  655. struct HvCallPci_LoadReturn ret;
  656. struct iSeries_Device_Node *DevNode =
  657. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  658. if (DevNode == NULL) {
  659. static unsigned long last_jiffies;
  660. static int num_printed;
  661. if ((jiffies - last_jiffies) > 60 * HZ) {
  662. last_jiffies = jiffies;
  663. num_printed = 0;
  664. }
  665. if (num_printed++ < 10)
  666. printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
  667. return 0xff;
  668. }
  669. do {
  670. ++Pci_Io_Read_Count;
  671. HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
  672. } while (CheckReturnCode("RDB", DevNode, ret.rc) != 0);
  673. return (u8)ret.value;
  674. }
  675. EXPORT_SYMBOL(iSeries_Read_Byte);
  676. u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
  677. {
  678. u64 BarOffset;
  679. u64 dsa;
  680. struct HvCallPci_LoadReturn ret;
  681. struct iSeries_Device_Node *DevNode =
  682. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  683. if (DevNode == NULL) {
  684. static unsigned long last_jiffies;
  685. static int num_printed;
  686. if ((jiffies - last_jiffies) > 60 * HZ) {
  687. last_jiffies = jiffies;
  688. num_printed = 0;
  689. }
  690. if (num_printed++ < 10)
  691. printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
  692. return 0xffff;
  693. }
  694. do {
  695. ++Pci_Io_Read_Count;
  696. HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
  697. BarOffset, 0);
  698. } while (CheckReturnCode("RDW", DevNode, ret.rc) != 0);
  699. return swab16((u16)ret.value);
  700. }
  701. EXPORT_SYMBOL(iSeries_Read_Word);
  702. u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
  703. {
  704. u64 BarOffset;
  705. u64 dsa;
  706. struct HvCallPci_LoadReturn ret;
  707. struct iSeries_Device_Node *DevNode =
  708. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  709. if (DevNode == NULL) {
  710. static unsigned long last_jiffies;
  711. static int num_printed;
  712. if ((jiffies - last_jiffies) > 60 * HZ) {
  713. last_jiffies = jiffies;
  714. num_printed = 0;
  715. }
  716. if (num_printed++ < 10)
  717. printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
  718. return 0xffffffff;
  719. }
  720. do {
  721. ++Pci_Io_Read_Count;
  722. HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
  723. BarOffset, 0);
  724. } while (CheckReturnCode("RDL", DevNode, ret.rc) != 0);
  725. return swab32((u32)ret.value);
  726. }
  727. EXPORT_SYMBOL(iSeries_Read_Long);
  728. /*
  729. * Write MM I/O Instructions for the iSeries
  730. *
  731. * iSeries_Write_Byte = Write Byte (8 bit)
  732. * iSeries_Write_Word = Write Word(16 bit)
  733. * iSeries_Write_Long = Write Long(32 bit)
  734. */
  735. void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
  736. {
  737. u64 BarOffset;
  738. u64 dsa;
  739. u64 rc;
  740. struct iSeries_Device_Node *DevNode =
  741. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  742. if (DevNode == NULL) {
  743. static unsigned long last_jiffies;
  744. static int num_printed;
  745. if ((jiffies - last_jiffies) > 60 * HZ) {
  746. last_jiffies = jiffies;
  747. num_printed = 0;
  748. }
  749. if (num_printed++ < 10)
  750. printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
  751. return;
  752. }
  753. do {
  754. ++Pci_Io_Write_Count;
  755. rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
  756. } while (CheckReturnCode("WWB", DevNode, rc) != 0);
  757. }
  758. EXPORT_SYMBOL(iSeries_Write_Byte);
  759. void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
  760. {
  761. u64 BarOffset;
  762. u64 dsa;
  763. u64 rc;
  764. struct iSeries_Device_Node *DevNode =
  765. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  766. if (DevNode == NULL) {
  767. static unsigned long last_jiffies;
  768. static int num_printed;
  769. if ((jiffies - last_jiffies) > 60 * HZ) {
  770. last_jiffies = jiffies;
  771. num_printed = 0;
  772. }
  773. if (num_printed++ < 10)
  774. printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
  775. return;
  776. }
  777. do {
  778. ++Pci_Io_Write_Count;
  779. rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
  780. } while (CheckReturnCode("WWW", DevNode, rc) != 0);
  781. }
  782. EXPORT_SYMBOL(iSeries_Write_Word);
  783. void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
  784. {
  785. u64 BarOffset;
  786. u64 dsa;
  787. u64 rc;
  788. struct iSeries_Device_Node *DevNode =
  789. xlate_iomm_address(IoAddress, &dsa, &BarOffset);
  790. if (DevNode == NULL) {
  791. static unsigned long last_jiffies;
  792. static int num_printed;
  793. if ((jiffies - last_jiffies) > 60 * HZ) {
  794. last_jiffies = jiffies;
  795. num_printed = 0;
  796. }
  797. if (num_printed++ < 10)
  798. printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
  799. return;
  800. }
  801. do {
  802. ++Pci_Io_Write_Count;
  803. rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
  804. } while (CheckReturnCode("WWL", DevNode, rc) != 0);
  805. }
  806. EXPORT_SYMBOL(iSeries_Write_Long);