conf_space_header.c 9.5 KB

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