conf_space_header.c 8.5 KB

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