pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/capability.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/irq.h>
  24. #include <linux/io.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/processor.h>
  27. #include <asm/sections.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/hv_driver.h>
  30. #include <hv/drv_pcie_rc_intf.h>
  31. /*
  32. * Initialization flow and process
  33. * -------------------------------
  34. *
  35. * This files contains the routines to search for PCI buses,
  36. * enumerate the buses, and configure any attached devices.
  37. *
  38. * There are two entry points here:
  39. * 1) tile_pci_init
  40. * This sets up the pci_controller structs, and opens the
  41. * FDs to the hypervisor. This is called from setup_arch() early
  42. * in the boot process.
  43. * 2) pcibios_init
  44. * This probes the PCI bus(es) for any attached hardware. It's
  45. * called by subsys_initcall. All of the real work is done by the
  46. * generic Linux PCI layer.
  47. *
  48. */
  49. /*
  50. * This flag tells if the platform is TILEmpower that needs
  51. * special configuration for the PLX switch chip.
  52. */
  53. int __write_once tile_plx_gen1;
  54. static struct pci_controller controllers[TILE_NUM_PCIE];
  55. static int num_controllers;
  56. static int pci_scan_flags[TILE_NUM_PCIE];
  57. static struct pci_ops tile_cfg_ops;
  58. /*
  59. * We don't need to worry about the alignment of resources.
  60. */
  61. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  62. resource_size_t size, resource_size_t align)
  63. {
  64. return res->start;
  65. }
  66. EXPORT_SYMBOL(pcibios_align_resource);
  67. /*
  68. * Open a FD to the hypervisor PCI device.
  69. *
  70. * controller_id is the controller number, config type is 0 or 1 for
  71. * config0 or config1 operations.
  72. */
  73. static int __devinit tile_pcie_open(int controller_id, int config_type)
  74. {
  75. char filename[32];
  76. int fd;
  77. sprintf(filename, "pcie/%d/config%d", controller_id, config_type);
  78. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  79. return fd;
  80. }
  81. /*
  82. * Get the IRQ numbers from the HV and set up the handlers for them.
  83. */
  84. static int __devinit tile_init_irqs(int controller_id,
  85. struct pci_controller *controller)
  86. {
  87. char filename[32];
  88. int fd;
  89. int ret;
  90. int x;
  91. struct pcie_rc_config rc_config;
  92. sprintf(filename, "pcie/%d/ctl", controller_id);
  93. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  94. if (fd < 0) {
  95. pr_err("PCI: hv_dev_open(%s) failed\n", filename);
  96. return -1;
  97. }
  98. ret = hv_dev_pread(fd, 0, (HV_VirtAddr)(&rc_config),
  99. sizeof(rc_config), PCIE_RC_CONFIG_MASK_OFF);
  100. hv_dev_close(fd);
  101. if (ret != sizeof(rc_config)) {
  102. pr_err("PCI: wanted %zd bytes, got %d\n",
  103. sizeof(rc_config), ret);
  104. return -1;
  105. }
  106. /* Record irq_base so that we can map INTx to IRQ # later. */
  107. controller->irq_base = rc_config.intr;
  108. for (x = 0; x < 4; x++)
  109. tile_irq_activate(rc_config.intr + x,
  110. TILE_IRQ_HW_CLEAR);
  111. if (rc_config.plx_gen1)
  112. controller->plx_gen1 = 1;
  113. return 0;
  114. }
  115. /*
  116. * First initialization entry point, called from setup_arch().
  117. *
  118. * Find valid controllers and fill in pci_controller structs for each
  119. * of them.
  120. *
  121. * Returns the number of controllers discovered.
  122. */
  123. int __devinit tile_pci_init(void)
  124. {
  125. int i;
  126. pr_info("PCI: Searching for controllers...\n");
  127. /* Re-init number of PCIe controllers to support hot-plug feature. */
  128. num_controllers = 0;
  129. /* Do any configuration we need before using the PCIe */
  130. for (i = 0; i < TILE_NUM_PCIE; i++) {
  131. /*
  132. * To see whether we need a real config op based on
  133. * the results of pcibios_init(), to support PCIe hot-plug.
  134. */
  135. if (pci_scan_flags[i] == 0) {
  136. int hv_cfg_fd0 = -1;
  137. int hv_cfg_fd1 = -1;
  138. int hv_mem_fd = -1;
  139. char name[32];
  140. struct pci_controller *controller;
  141. /*
  142. * Open the fd to the HV. If it fails then this
  143. * device doesn't exist.
  144. */
  145. hv_cfg_fd0 = tile_pcie_open(i, 0);
  146. if (hv_cfg_fd0 < 0)
  147. continue;
  148. hv_cfg_fd1 = tile_pcie_open(i, 1);
  149. if (hv_cfg_fd1 < 0) {
  150. pr_err("PCI: Couldn't open config fd to HV "
  151. "for controller %d\n", i);
  152. goto err_cont;
  153. }
  154. sprintf(name, "pcie/%d/mem", i);
  155. hv_mem_fd = hv_dev_open((HV_VirtAddr)name, 0);
  156. if (hv_mem_fd < 0) {
  157. pr_err("PCI: Could not open mem fd to HV!\n");
  158. goto err_cont;
  159. }
  160. pr_info("PCI: Found PCI controller #%d\n", i);
  161. controller = &controllers[i];
  162. controller->index = i;
  163. controller->hv_cfg_fd[0] = hv_cfg_fd0;
  164. controller->hv_cfg_fd[1] = hv_cfg_fd1;
  165. controller->hv_mem_fd = hv_mem_fd;
  166. controller->first_busno = 0;
  167. controller->last_busno = 0xff;
  168. controller->ops = &tile_cfg_ops;
  169. num_controllers++;
  170. continue;
  171. err_cont:
  172. if (hv_cfg_fd0 >= 0)
  173. hv_dev_close(hv_cfg_fd0);
  174. if (hv_cfg_fd1 >= 0)
  175. hv_dev_close(hv_cfg_fd1);
  176. if (hv_mem_fd >= 0)
  177. hv_dev_close(hv_mem_fd);
  178. continue;
  179. }
  180. }
  181. /*
  182. * Before using the PCIe, see if we need to do any platform-specific
  183. * configuration, such as the PLX switch Gen 1 issue on TILEmpower.
  184. */
  185. for (i = 0; i < num_controllers; i++) {
  186. struct pci_controller *controller = &controllers[i];
  187. if (controller->plx_gen1)
  188. tile_plx_gen1 = 1;
  189. }
  190. return num_controllers;
  191. }
  192. /*
  193. * (pin - 1) converts from the PCI standard's [1:4] convention to
  194. * a normal [0:3] range.
  195. */
  196. static int tile_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  197. {
  198. struct pci_controller *controller =
  199. (struct pci_controller *)dev->sysdata;
  200. return (pin - 1) + controller->irq_base;
  201. }
  202. static void __devinit fixup_read_and_payload_sizes(void)
  203. {
  204. struct pci_dev *dev = NULL;
  205. int smallest_max_payload = 0x1; /* Tile maxes out at 256 bytes. */
  206. int max_read_size = 0x2; /* Limit to 512 byte reads. */
  207. u16 new_values;
  208. /* Scan for the smallest maximum payload size. */
  209. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  210. int pcie_caps_offset;
  211. u32 devcap;
  212. int max_payload;
  213. pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
  214. if (pcie_caps_offset == 0)
  215. continue;
  216. pci_read_config_dword(dev, pcie_caps_offset + PCI_EXP_DEVCAP,
  217. &devcap);
  218. max_payload = devcap & PCI_EXP_DEVCAP_PAYLOAD;
  219. if (max_payload < smallest_max_payload)
  220. smallest_max_payload = max_payload;
  221. }
  222. /* Now, set the max_payload_size for all devices to that value. */
  223. new_values = (max_read_size << 12) | (smallest_max_payload << 5);
  224. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  225. int pcie_caps_offset;
  226. u16 devctl;
  227. pcie_caps_offset = pci_find_capability(dev, PCI_CAP_ID_EXP);
  228. if (pcie_caps_offset == 0)
  229. continue;
  230. pci_read_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
  231. &devctl);
  232. devctl &= ~(PCI_EXP_DEVCTL_PAYLOAD | PCI_EXP_DEVCTL_READRQ);
  233. devctl |= new_values;
  234. pci_write_config_word(dev, pcie_caps_offset + PCI_EXP_DEVCTL,
  235. devctl);
  236. }
  237. }
  238. /*
  239. * Second PCI initialization entry point, called by subsys_initcall.
  240. *
  241. * The controllers have been set up by the time we get here, by a call to
  242. * tile_pci_init.
  243. */
  244. int __devinit pcibios_init(void)
  245. {
  246. int i;
  247. pr_info("PCI: Probing PCI hardware\n");
  248. /*
  249. * Delay a bit in case devices aren't ready. Some devices are
  250. * known to require at least 20ms here, but we use a more
  251. * conservative value.
  252. */
  253. mdelay(250);
  254. /* Scan all of the recorded PCI controllers. */
  255. for (i = 0; i < TILE_NUM_PCIE; i++) {
  256. /*
  257. * Do real pcibios init ops if the controller is initialized
  258. * by tile_pci_init() successfully and not initialized by
  259. * pcibios_init() yet to support PCIe hot-plug.
  260. */
  261. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  262. struct pci_controller *controller = &controllers[i];
  263. struct pci_bus *bus;
  264. if (tile_init_irqs(i, controller)) {
  265. pr_err("PCI: Could not initialize IRQs\n");
  266. continue;
  267. }
  268. pr_info("PCI: initializing controller #%d\n", i);
  269. /*
  270. * This comes from the generic Linux PCI driver.
  271. *
  272. * It reads the PCI tree for this bus into the Linux
  273. * data structures.
  274. *
  275. * This is inlined in linux/pci.h and calls into
  276. * pci_scan_bus_parented() in probe.c.
  277. */
  278. bus = pci_scan_bus(0, controller->ops, controller);
  279. controller->root_bus = bus;
  280. controller->last_busno = bus->subordinate;
  281. }
  282. }
  283. /* Do machine dependent PCI interrupt routing */
  284. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  285. /*
  286. * This comes from the generic Linux PCI driver.
  287. *
  288. * It allocates all of the resources (I/O memory, etc)
  289. * associated with the devices read in above.
  290. */
  291. pci_assign_unassigned_resources();
  292. /* Configure the max_read_size and max_payload_size values. */
  293. fixup_read_and_payload_sizes();
  294. /* Record the I/O resources in the PCI controller structure. */
  295. for (i = 0; i < TILE_NUM_PCIE; i++) {
  296. /*
  297. * Do real pcibios init ops if the controller is initialized
  298. * by tile_pci_init() successfully and not initialized by
  299. * pcibios_init() yet to support PCIe hot-plug.
  300. */
  301. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  302. struct pci_bus *root_bus = controllers[i].root_bus;
  303. struct pci_bus *next_bus;
  304. struct pci_dev *dev;
  305. list_for_each_entry(dev, &root_bus->devices, bus_list) {
  306. /*
  307. * Find the PCI host controller, ie. the 1st
  308. * bridge.
  309. */
  310. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  311. (PCI_SLOT(dev->devfn) == 0)) {
  312. next_bus = dev->subordinate;
  313. controllers[i].mem_resources[0] =
  314. *next_bus->resource[0];
  315. controllers[i].mem_resources[1] =
  316. *next_bus->resource[1];
  317. controllers[i].mem_resources[2] =
  318. *next_bus->resource[2];
  319. /* Setup flags. */
  320. pci_scan_flags[i] = 1;
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. return 0;
  327. }
  328. subsys_initcall(pcibios_init);
  329. /*
  330. * No bus fixups needed.
  331. */
  332. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  333. {
  334. /* Nothing needs to be done. */
  335. }
  336. /*
  337. * This can be called from the generic PCI layer, but doesn't need to
  338. * do anything.
  339. */
  340. char __devinit *pcibios_setup(char *str)
  341. {
  342. /* Nothing needs to be done. */
  343. return str;
  344. }
  345. /*
  346. * This is called from the generic Linux layer.
  347. */
  348. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  349. {
  350. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  351. }
  352. /*
  353. * Enable memory and/or address decoding, as appropriate, for the
  354. * device described by the 'dev' struct.
  355. *
  356. * This is called from the generic PCI layer, and can be called
  357. * for bridges or endpoints.
  358. */
  359. int pcibios_enable_device(struct pci_dev *dev, int mask)
  360. {
  361. u16 cmd, old_cmd;
  362. u8 header_type;
  363. int i;
  364. struct resource *r;
  365. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  366. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  367. old_cmd = cmd;
  368. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  369. /*
  370. * For bridges, we enable both memory and I/O decoding
  371. * in call cases.
  372. */
  373. cmd |= PCI_COMMAND_IO;
  374. cmd |= PCI_COMMAND_MEMORY;
  375. } else {
  376. /*
  377. * For endpoints, we enable memory and/or I/O decoding
  378. * only if they have a memory resource of that type.
  379. */
  380. for (i = 0; i < 6; i++) {
  381. r = &dev->resource[i];
  382. if (r->flags & IORESOURCE_UNSET) {
  383. pr_err("PCI: Device %s not available "
  384. "because of resource collisions\n",
  385. pci_name(dev));
  386. return -EINVAL;
  387. }
  388. if (r->flags & IORESOURCE_IO)
  389. cmd |= PCI_COMMAND_IO;
  390. if (r->flags & IORESOURCE_MEM)
  391. cmd |= PCI_COMMAND_MEMORY;
  392. }
  393. }
  394. /*
  395. * We only write the command if it changed.
  396. */
  397. if (cmd != old_cmd)
  398. pci_write_config_word(dev, PCI_COMMAND, cmd);
  399. return 0;
  400. }
  401. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
  402. {
  403. unsigned long start = pci_resource_start(dev, bar);
  404. unsigned long len = pci_resource_len(dev, bar);
  405. unsigned long flags = pci_resource_flags(dev, bar);
  406. if (!len)
  407. return NULL;
  408. if (max && len > max)
  409. len = max;
  410. if (!(flags & IORESOURCE_MEM)) {
  411. pr_info("PCI: Trying to map invalid resource %#lx\n", flags);
  412. start = 0;
  413. }
  414. return (void __iomem *)start;
  415. }
  416. EXPORT_SYMBOL(pci_iomap);
  417. /****************************************************************
  418. *
  419. * Tile PCI config space read/write routines
  420. *
  421. ****************************************************************/
  422. /*
  423. * These are the normal read and write ops
  424. * These are expanded with macros from pci_bus_read_config_byte() etc.
  425. *
  426. * devfn is the combined PCI slot & function.
  427. *
  428. * offset is in bytes, from the start of config space for the
  429. * specified bus & slot.
  430. */
  431. static int __devinit tile_cfg_read(struct pci_bus *bus,
  432. unsigned int devfn,
  433. int offset,
  434. int size,
  435. u32 *val)
  436. {
  437. struct pci_controller *controller = bus->sysdata;
  438. int busnum = bus->number & 0xff;
  439. int slot = (devfn >> 3) & 0x1f;
  440. int function = devfn & 0x7;
  441. u32 addr;
  442. int config_mode = 1;
  443. /*
  444. * There is no bridge between the Tile and bus 0, so we
  445. * use config0 to talk to bus 0.
  446. *
  447. * If we're talking to a bus other than zero then we
  448. * must have found a bridge.
  449. */
  450. if (busnum == 0) {
  451. /*
  452. * We fake an empty slot for (busnum == 0) && (slot > 0),
  453. * since there is only one slot on bus 0.
  454. */
  455. if (slot) {
  456. *val = 0xFFFFFFFF;
  457. return 0;
  458. }
  459. config_mode = 0;
  460. }
  461. addr = busnum << 20; /* Bus in 27:20 */
  462. addr |= slot << 15; /* Slot (device) in 19:15 */
  463. addr |= function << 12; /* Function is in 14:12 */
  464. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  465. return hv_dev_pread(controller->hv_cfg_fd[config_mode], 0,
  466. (HV_VirtAddr)(val), size, addr);
  467. }
  468. /*
  469. * See tile_cfg_read() for relevant comments.
  470. * Note that "val" is the value to write, not a pointer to that value.
  471. */
  472. static int __devinit tile_cfg_write(struct pci_bus *bus,
  473. unsigned int devfn,
  474. int offset,
  475. int size,
  476. u32 val)
  477. {
  478. struct pci_controller *controller = bus->sysdata;
  479. int busnum = bus->number & 0xff;
  480. int slot = (devfn >> 3) & 0x1f;
  481. int function = devfn & 0x7;
  482. u32 addr;
  483. int config_mode = 1;
  484. HV_VirtAddr valp = (HV_VirtAddr)&val;
  485. /*
  486. * For bus 0 slot 0 we use config 0 accesses.
  487. */
  488. if (busnum == 0) {
  489. /*
  490. * We fake an empty slot for (busnum == 0) && (slot > 0),
  491. * since there is only one slot on bus 0.
  492. */
  493. if (slot)
  494. return 0;
  495. config_mode = 0;
  496. }
  497. addr = busnum << 20; /* Bus in 27:20 */
  498. addr |= slot << 15; /* Slot (device) in 19:15 */
  499. addr |= function << 12; /* Function is in 14:12 */
  500. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  501. #ifdef __BIG_ENDIAN
  502. /* Point to the correct part of the 32-bit "val". */
  503. valp += 4 - size;
  504. #endif
  505. return hv_dev_pwrite(controller->hv_cfg_fd[config_mode], 0,
  506. valp, size, addr);
  507. }
  508. static struct pci_ops tile_cfg_ops = {
  509. .read = tile_cfg_read,
  510. .write = tile_cfg_write,
  511. };
  512. /*
  513. * In the following, each PCI controller's mem_resources[1]
  514. * represents its (non-prefetchable) PCI memory resource.
  515. * mem_resources[0] and mem_resources[2] refer to its PCI I/O and
  516. * prefetchable PCI memory resources, respectively.
  517. * For more details, see pci_setup_bridge() in setup-bus.c.
  518. * By comparing the target PCI memory address against the
  519. * end address of controller 0, we can determine the controller
  520. * that should accept the PCI memory access.
  521. */
  522. #define TILE_READ(size, type) \
  523. type _tile_read##size(unsigned long addr) \
  524. { \
  525. type val; \
  526. int idx = 0; \
  527. if (addr > controllers[0].mem_resources[1].end && \
  528. addr > controllers[0].mem_resources[2].end) \
  529. idx = 1; \
  530. if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
  531. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  532. pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
  533. sizeof(type), addr); \
  534. return val; \
  535. } \
  536. EXPORT_SYMBOL(_tile_read##size)
  537. TILE_READ(b, u8);
  538. TILE_READ(w, u16);
  539. TILE_READ(l, u32);
  540. TILE_READ(q, u64);
  541. #define TILE_WRITE(size, type) \
  542. void _tile_write##size(type val, unsigned long addr) \
  543. { \
  544. int idx = 0; \
  545. if (addr > controllers[0].mem_resources[1].end && \
  546. addr > controllers[0].mem_resources[2].end) \
  547. idx = 1; \
  548. if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
  549. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  550. pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
  551. sizeof(type), addr); \
  552. } \
  553. EXPORT_SYMBOL(_tile_write##size)
  554. TILE_WRITE(b, u8);
  555. TILE_WRITE(w, u16);
  556. TILE_WRITE(l, u32);
  557. TILE_WRITE(q, u64);