blob: 6b1e25159598b6b5ae50c9d1861ecfb37f83f6a1 [file] [log] [blame]
Dmitry Fleytman75020a72013-03-09 11:21:04 +02001/*
2 * QEMU VMWARE paravirtual devices - auxiliary code
3 *
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
5 *
6 * Developed by Daynix Computing LTD (http://www.daynix.com)
7 *
8 * Authors:
9 * Dmitry Fleytman <dmitry@daynix.com>
10 * Yan Vugenfirer <yan@daynix.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 */
16
17#ifndef VMWARE_UTILS_H
18#define VMWARE_UTILS_H
19
20#include "qemu/range.h"
Miao Yandd3c1682015-12-07 21:28:33 -080021#include "vmxnet_debug.h"
Dmitry Fleytman75020a72013-03-09 11:21:04 +020022
23/*
24 * Shared memory access functions with byte swap support
25 * Each function contains printout for reverse-engineering needs
26 *
27 */
28static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020029vmw_shmem_read(PCIDevice *d, hwaddr addr, void *buf, int len)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020030{
31 VMW_SHPRN("SHMEM r: %" PRIx64 ", len: %d to %p", addr, len, buf);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020032 pci_dma_read(d, addr, buf, len);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020033}
34
35static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020036vmw_shmem_write(PCIDevice *d, hwaddr addr, void *buf, int len)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020037{
38 VMW_SHPRN("SHMEM w: %" PRIx64 ", len: %d to %p", addr, len, buf);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020039 pci_dma_write(d, addr, buf, len);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020040}
41
42static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020043vmw_shmem_rw(PCIDevice *d, hwaddr addr, void *buf, int len, int is_write)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020044{
45 VMW_SHPRN("SHMEM r/w: %" PRIx64 ", len: %d (to %p), is write: %d",
46 addr, len, buf, is_write);
47
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020048 if (is_write)
49 pci_dma_write(d, addr, buf, len);
50 else
51 pci_dma_read(d, addr, buf, len);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020052}
53
54static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020055vmw_shmem_set(PCIDevice *d, hwaddr addr, uint8_t val, int len)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020056{
57 int i;
58 VMW_SHPRN("SHMEM set: %" PRIx64 ", len: %d (value 0x%X)", addr, len, val);
59
60 for (i = 0; i < len; i++) {
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020061 pci_dma_write(d, addr + i, &val, 1);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020062 }
63}
64
65static inline uint32_t
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020066vmw_shmem_ld8(PCIDevice *d, hwaddr addr)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020067{
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020068 uint8_t res;
69 pci_dma_read(d, addr, &res, 1);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020070 VMW_SHPRN("SHMEM load8: %" PRIx64 " (value 0x%X)", addr, res);
71 return res;
72}
73
74static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020075vmw_shmem_st8(PCIDevice *d, hwaddr addr, uint8_t value)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020076{
77 VMW_SHPRN("SHMEM store8: %" PRIx64 " (value 0x%X)", addr, value);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020078 pci_dma_write(d, addr, &value, 1);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020079}
80
81static inline uint32_t
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020082vmw_shmem_ld16(PCIDevice *d, hwaddr addr)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020083{
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020084 uint16_t res;
85 pci_dma_read(d, addr, &res, 2);
Thomas Huthc527e0a2017-11-14 12:20:24 +010086 res = le16_to_cpu(res);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020087 VMW_SHPRN("SHMEM load16: %" PRIx64 " (value 0x%X)", addr, res);
88 return res;
89}
90
91static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020092vmw_shmem_st16(PCIDevice *d, hwaddr addr, uint16_t value)
Dmitry Fleytman75020a72013-03-09 11:21:04 +020093{
94 VMW_SHPRN("SHMEM store16: %" PRIx64 " (value 0x%X)", addr, value);
Thomas Huthc527e0a2017-11-14 12:20:24 +010095 value = cpu_to_le16(value);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +020096 pci_dma_write(d, addr, &value, 2);
Dmitry Fleytman75020a72013-03-09 11:21:04 +020097}
98
99static inline uint32_t
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200100vmw_shmem_ld32(PCIDevice *d, hwaddr addr)
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200101{
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200102 uint32_t res;
103 pci_dma_read(d, addr, &res, 4);
Thomas Huthc527e0a2017-11-14 12:20:24 +0100104 res = le32_to_cpu(res);
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200105 VMW_SHPRN("SHMEM load32: %" PRIx64 " (value 0x%X)", addr, res);
106 return res;
107}
108
109static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200110vmw_shmem_st32(PCIDevice *d, hwaddr addr, uint32_t value)
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200111{
112 VMW_SHPRN("SHMEM store32: %" PRIx64 " (value 0x%X)", addr, value);
Thomas Huthc527e0a2017-11-14 12:20:24 +0100113 value = cpu_to_le32(value);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200114 pci_dma_write(d, addr, &value, 4);
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200115}
116
117static inline uint64_t
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200118vmw_shmem_ld64(PCIDevice *d, hwaddr addr)
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200119{
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200120 uint64_t res;
121 pci_dma_read(d, addr, &res, 8);
Thomas Huthc527e0a2017-11-14 12:20:24 +0100122 res = le64_to_cpu(res);
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200123 VMW_SHPRN("SHMEM load64: %" PRIx64 " (value %" PRIx64 ")", addr, res);
124 return res;
125}
126
127static inline void
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200128vmw_shmem_st64(PCIDevice *d, hwaddr addr, uint64_t value)
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200129{
130 VMW_SHPRN("SHMEM store64: %" PRIx64 " (value %" PRIx64 ")", addr, value);
Thomas Huthc527e0a2017-11-14 12:20:24 +0100131 value = cpu_to_le64(value);
KarimAllah Ahmedc5082772016-06-20 15:50:40 +0200132 pci_dma_write(d, addr, &value, 8);
Dmitry Fleytman75020a72013-03-09 11:21:04 +0200133}
134
135/* Macros for simplification of operations on array-style registers */
136
137/*
138 * Whether <addr> lies inside of array-style register defined by <base>,
139 * number of elements (<cnt>) and element size (<regsize>)
140 *
141*/
142#define VMW_IS_MULTIREG_ADDR(addr, base, cnt, regsize) \
143 range_covers_byte(base, cnt * regsize, addr)
144
145/*
146 * Returns index of given register (<addr>) in array-style register defined by
147 * <base> and element size (<regsize>)
148 *
149*/
150#define VMW_MULTIREG_IDX_BY_ADDR(addr, base, regsize) \
151 (((addr) - (base)) / (regsize))
152
153#endif