conf_space_header.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * PCI Backend - Handles the virtual fields in the configuration space headers.
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/pci.h>
  8. #include "pciback.h"
  9. #include "conf_space.h"
  10. struct pci_bar_info {
  11. u32 val;
  12. u32 len_val;
  13. int which;
  14. };
  15. #define DRV_NAME "xen-pciback"
  16. #define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
  17. #define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
  18. static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
  19. {
  20. int i;
  21. int ret;
  22. ret = xen_pcibk_read_config_word(dev, offset, value, data);
  23. if (!atomic_read(&dev->enable_cnt))
  24. return ret;
  25. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  26. if (dev->resource[i].flags & IORESOURCE_IO)
  27. *value |= PCI_COMMAND_IO;
  28. if (dev->resource[i].flags & IORESOURCE_MEM)
  29. *value |= PCI_COMMAND_MEMORY;
  30. }
  31. return ret;
  32. }
  33. static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
  34. {
  35. struct xen_pcibk_dev_data *dev_data;
  36. int err;
  37. dev_data = pci_get_drvdata(dev);
  38. if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
  39. if (unlikely(verbose_request))
  40. printk(KERN_DEBUG DRV_NAME ": %s: enable\n",
  41. pci_name(dev));
  42. err = pci_enable_device(dev);
  43. if (err)
  44. return err;
  45. if (dev_data)
  46. dev_data->enable_intx = 1;
  47. } else if (pci_is_enabled(dev) && !is_enable_cmd(value)) {
  48. if (unlikely(verbose_request))
  49. printk(KERN_DEBUG DRV_NAME ": %s: disable\n",
  50. pci_name(dev));
  51. pci_disable_device(dev);
  52. if (dev_data)
  53. dev_data->enable_intx = 0;
  54. }
  55. if (!dev->is_busmaster && is_master_cmd(value)) {
  56. if (unlikely(verbose_request))
  57. printk(KERN_DEBUG DRV_NAME ": %s: set bus master\n",
  58. pci_name(dev));
  59. pci_set_master(dev);
  60. }
  61. if (value & PCI_COMMAND_INVALIDATE) {
  62. if (unlikely(verbose_request))
  63. printk(KERN_DEBUG
  64. DRV_NAME ": %s: enable memory-write-invalidate\n",
  65. pci_name(dev));
  66. err = pci_set_mwi(dev);
  67. if (err) {
  68. printk(KERN_WARNING
  69. DRV_NAME ": %s: cannot enable "
  70. "memory-write-invalidate (%d)\n",
  71. pci_name(dev), err);
  72. value &= ~PCI_COMMAND_INVALIDATE;
  73. }
  74. }
  75. return pci_write_config_word(dev, offset, value);
  76. }
  77. static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
  78. {
  79. struct pci_bar_info *bar = data;
  80. if (unlikely(!bar)) {
  81. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  82. pci_name(dev));
  83. return XEN_PCI_ERR_op_failed;
  84. }
  85. /* A write to obtain the length must happen as a 32-bit write.
  86. * This does not (yet) support writing individual bytes
  87. */
  88. if (value == ~PCI_ROM_ADDRESS_ENABLE)
  89. bar->which = 1;
  90. else {
  91. u32 tmpval;
  92. pci_read_config_dword(dev, offset, &tmpval);
  93. if (tmpval != bar->val && value == bar->val) {
  94. /* Allow restoration of bar value. */
  95. pci_write_config_dword(dev, offset, bar->val);
  96. }
  97. bar->which = 0;
  98. }
  99. /* Do we need to support enabling/disabling the rom address here? */
  100. return 0;
  101. }
  102. /* For the BARs, only allow writes which write ~0 or
  103. * the correct resource information
  104. * (Needed for when the driver probes the resource usage)
  105. */
  106. static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
  107. {
  108. struct pci_bar_info *bar = data;
  109. if (unlikely(!bar)) {
  110. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  111. pci_name(dev));
  112. return XEN_PCI_ERR_op_failed;
  113. }
  114. /* A write to obtain the length must happen as a 32-bit write.
  115. * This does not (yet) support writing individual bytes
  116. */
  117. if (value == ~0)
  118. bar->which = 1;
  119. else {
  120. u32 tmpval;
  121. pci_read_config_dword(dev, offset, &tmpval);
  122. if (tmpval != bar->val && value == bar->val) {
  123. /* Allow restoration of bar value. */
  124. pci_write_config_dword(dev, offset, bar->val);
  125. }
  126. bar->which = 0;
  127. }
  128. return 0;
  129. }
  130. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  131. {
  132. struct pci_bar_info *bar = data;
  133. if (unlikely(!bar)) {
  134. printk(KERN_WARNING DRV_NAME ": driver data not found for %s\n",
  135. pci_name(dev));
  136. return XEN_PCI_ERR_op_failed;
  137. }
  138. *value = bar->which ? bar->len_val : bar->val;
  139. return 0;
  140. }
  141. static inline void read_dev_bar(struct pci_dev *dev,
  142. struct pci_bar_info *bar_info, int offset,
  143. u32 len_mask)
  144. {
  145. int pos;
  146. struct resource *res = dev->resource;
  147. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  148. pos = PCI_ROM_RESOURCE;
  149. else {
  150. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  151. if (pos && ((res[pos - 1].flags & (PCI_BASE_ADDRESS_SPACE |
  152. PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  153. (PCI_BASE_ADDRESS_SPACE_MEMORY |
  154. PCI_BASE_ADDRESS_MEM_TYPE_64))) {
  155. bar_info->val = res[pos - 1].start >> 32;
  156. bar_info->len_val = res[pos - 1].end >> 32;
  157. return;
  158. }
  159. }
  160. bar_info->val = res[pos].start |
  161. (res[pos].flags & PCI_REGION_FLAG_MASK);
  162. bar_info->len_val = res[pos].end - res[pos].start + 1;
  163. }
  164. static void *bar_init(struct pci_dev *dev, int offset)
  165. {
  166. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  167. if (!bar)
  168. return ERR_PTR(-ENOMEM);
  169. read_dev_bar(dev, bar, offset, ~0);
  170. bar->which = 0;
  171. return bar;
  172. }
  173. static void *rom_init(struct pci_dev *dev, int offset)
  174. {
  175. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  176. if (!bar)
  177. return ERR_PTR(-ENOMEM);
  178. read_dev_bar(dev, bar, offset, ~PCI_ROM_ADDRESS_ENABLE);
  179. bar->which = 0;
  180. return bar;
  181. }
  182. static void bar_reset(struct pci_dev *dev, int offset, void *data)
  183. {
  184. struct pci_bar_info *bar = data;
  185. bar->which = 0;
  186. }
  187. static void bar_release(struct pci_dev *dev, int offset, void *data)
  188. {
  189. kfree(data);
  190. }
  191. static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
  192. u16 *value, void *data)
  193. {
  194. *value = dev->vendor;
  195. return 0;
  196. }
  197. static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
  198. u16 *value, void *data)
  199. {
  200. *value = dev->device;
  201. return 0;
  202. }
  203. static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
  204. void *data)
  205. {
  206. *value = (u8) dev->irq;
  207. return 0;
  208. }
  209. static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
  210. {
  211. u8 cur_value;
  212. int err;
  213. err = pci_read_config_byte(dev, offset, &cur_value);
  214. if (err)
  215. goto out;
  216. if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
  217. || value == PCI_BIST_START)
  218. err = pci_write_config_byte(dev, offset, value);
  219. out:
  220. return err;
  221. }
  222. static const struct config_field header_common[] = {
  223. {
  224. .offset = PCI_VENDOR_ID,
  225. .size = 2,
  226. .u.w.read = xen_pcibk_read_vendor,
  227. },
  228. {
  229. .offset = PCI_DEVICE_ID,
  230. .size = 2,
  231. .u.w.read = xen_pcibk_read_device,
  232. },
  233. {
  234. .offset = PCI_COMMAND,
  235. .size = 2,
  236. .u.w.read = command_read,
  237. .u.w.write = command_write,
  238. },
  239. {
  240. .offset = PCI_INTERRUPT_LINE,
  241. .size = 1,
  242. .u.b.read = interrupt_read,
  243. },
  244. {
  245. .offset = PCI_INTERRUPT_PIN,
  246. .size = 1,
  247. .u.b.read = xen_pcibk_read_config_byte,
  248. },
  249. {
  250. /* Any side effects of letting driver domain control cache line? */
  251. .offset = PCI_CACHE_LINE_SIZE,
  252. .size = 1,
  253. .u.b.read = xen_pcibk_read_config_byte,
  254. .u.b.write = xen_pcibk_write_config_byte,
  255. },
  256. {
  257. .offset = PCI_LATENCY_TIMER,
  258. .size = 1,
  259. .u.b.read = xen_pcibk_read_config_byte,
  260. },
  261. {
  262. .offset = PCI_BIST,
  263. .size = 1,
  264. .u.b.read = xen_pcibk_read_config_byte,
  265. .u.b.write = bist_write,
  266. },
  267. {}
  268. };
  269. #define CFG_FIELD_BAR(reg_offset) \
  270. { \
  271. .offset = reg_offset, \
  272. .size = 4, \
  273. .init = bar_init, \
  274. .reset = bar_reset, \
  275. .release = bar_release, \
  276. .u.dw.read = bar_read, \
  277. .u.dw.write = bar_write, \
  278. }
  279. #define CFG_FIELD_ROM(reg_offset) \
  280. { \
  281. .offset = reg_offset, \
  282. .size = 4, \
  283. .init = rom_init, \
  284. .reset = bar_reset, \
  285. .release = bar_release, \
  286. .u.dw.read = bar_read, \
  287. .u.dw.write = rom_write, \
  288. }
  289. static const struct config_field header_0[] = {
  290. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  291. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  292. CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
  293. CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
  294. CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
  295. CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
  296. CFG_FIELD_ROM(PCI_ROM_ADDRESS),
  297. {}
  298. };
  299. static const struct config_field header_1[] = {
  300. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  301. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  302. CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
  303. {}
  304. };
  305. int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
  306. {
  307. int err;
  308. err = xen_pcibk_config_add_fields(dev, header_common);
  309. if (err)
  310. goto out;
  311. switch (dev->hdr_type) {
  312. case PCI_HEADER_TYPE_NORMAL:
  313. err = xen_pcibk_config_add_fields(dev, header_0);
  314. break;
  315. case PCI_HEADER_TYPE_BRIDGE:
  316. err = xen_pcibk_config_add_fields(dev, header_1);
  317. break;
  318. default:
  319. err = -EINVAL;
  320. printk(KERN_ERR DRV_NAME ": %s: Unsupported header type %d!\n",
  321. pci_name(dev), dev->hdr_type);
  322. break;
  323. }
  324. out:
  325. return err;
  326. }