]> Git Repo - qemu.git/blame - hw/ide/pci.c
rdma: fix up include directives
[qemu.git] / hw / ide / pci.c
CommitLineData
977e1244
GH
1/*
2 * QEMU IDE Emulation: PCI Bus support.
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
53239262 25#include "qemu/osdep.h"
a9c94277 26#include "hw/hw.h"
a9c94277
MA
27#include "hw/pci/pci.h"
28#include "hw/isa/isa.h"
4be74634 29#include "sysemu/block-backend.h"
9c17d615 30#include "sysemu/dma.h"
3251bdcf 31#include "qemu/error-report.h"
a9c94277 32#include "hw/ide/pci.h"
3eee2611 33#include "trace.h"
977e1244 34
40a6238a
AG
35#define BMDMA_PAGE_SIZE 4096
36
7e2648df 37#define BM_MIGRATION_COMPAT_STATUS_BITS \
fd648f10
PB
38 (IDE_RETRY_DMA | IDE_RETRY_PIO | \
39 IDE_RETRY_READ | IDE_RETRY_FLUSH)
7e2648df 40
40a6238a 41static void bmdma_start_dma(IDEDMA *dma, IDEState *s,
097310b5 42 BlockCompletionFunc *dma_cb)
40a6238a
AG
43{
44 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
45
40a6238a
AG
46 bm->dma_cb = dma_cb;
47 bm->cur_prd_last = 0;
48 bm->cur_prd_addr = 0;
49 bm->cur_prd_len = 0;
40a6238a
AG
50
51 if (bm->status & BM_STATUS_DMAING) {
52 bm->dma_cb(bmdma_active_if(bm), 0);
53 }
54}
55
3251bdcf 56/**
a718978e
JS
57 * Prepare an sglist based on available PRDs.
58 * @limit: How many bytes to prepare total.
59 *
60 * Returns the number of bytes prepared, -1 on error.
61 * IDEState.io_buffer_size will contain the number of bytes described
62 * by the PRDs, whether or not we added them to the sglist.
3251bdcf 63 */
a718978e 64static int32_t bmdma_prepare_buf(IDEDMA *dma, int32_t limit)
40a6238a
AG
65{
66 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
67 IDEState *s = bmdma_active_if(bm);
f6c11d56 68 PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
40a6238a
AG
69 struct {
70 uint32_t addr;
71 uint32_t size;
72 } prd;
73 int l, len;
74
f6c11d56 75 pci_dma_sglist_init(&s->sg, pci_dev,
552908fe 76 s->nsector / (BMDMA_PAGE_SIZE / 512) + 1);
40a6238a
AG
77 s->io_buffer_size = 0;
78 for(;;) {
79 if (bm->cur_prd_len == 0) {
80 /* end of table (with a fail safe of one page) */
81 if (bm->cur_prd_last ||
3251bdcf 82 (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
a718978e 83 return s->sg.size;
3251bdcf 84 }
f6c11d56 85 pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
40a6238a
AG
86 bm->cur_addr += 8;
87 prd.addr = le32_to_cpu(prd.addr);
88 prd.size = le32_to_cpu(prd.size);
89 len = prd.size & 0xfffe;
90 if (len == 0)
91 len = 0x10000;
92 bm->cur_prd_len = len;
93 bm->cur_prd_addr = prd.addr;
94 bm->cur_prd_last = (prd.size & 0x80000000);
95 }
96 l = bm->cur_prd_len;
97 if (l > 0) {
a718978e
JS
98 uint64_t sg_len;
99
100 /* Don't add extra bytes to the SGList; consume any remaining
101 * PRDs from the guest, but ignore them. */
102 sg_len = MIN(limit - s->sg.size, bm->cur_prd_len);
103 if (sg_len) {
104 qemu_sglist_add(&s->sg, bm->cur_prd_addr, sg_len);
105 }
3251bdcf 106
40a6238a
AG
107 bm->cur_prd_addr += l;
108 bm->cur_prd_len -= l;
109 s->io_buffer_size += l;
110 }
111 }
3251bdcf
JS
112
113 qemu_sglist_destroy(&s->sg);
114 s->io_buffer_size = 0;
115 return -1;
40a6238a
AG
116}
117
118/* return 0 if buffer completed */
119static int bmdma_rw_buf(IDEDMA *dma, int is_write)
120{
121 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
122 IDEState *s = bmdma_active_if(bm);
f6c11d56 123 PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
40a6238a
AG
124 struct {
125 uint32_t addr;
126 uint32_t size;
127 } prd;
128 int l, len;
129
130 for(;;) {
131 l = s->io_buffer_size - s->io_buffer_index;
132 if (l <= 0)
133 break;
134 if (bm->cur_prd_len == 0) {
135 /* end of table (with a fail safe of one page) */
136 if (bm->cur_prd_last ||
137 (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE)
138 return 0;
f6c11d56 139 pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
40a6238a
AG
140 bm->cur_addr += 8;
141 prd.addr = le32_to_cpu(prd.addr);
142 prd.size = le32_to_cpu(prd.size);
143 len = prd.size & 0xfffe;
144 if (len == 0)
145 len = 0x10000;
146 bm->cur_prd_len = len;
147 bm->cur_prd_addr = prd.addr;
148 bm->cur_prd_last = (prd.size & 0x80000000);
149 }
150 if (l > bm->cur_prd_len)
151 l = bm->cur_prd_len;
152 if (l > 0) {
153 if (is_write) {
f6c11d56 154 pci_dma_write(pci_dev, bm->cur_prd_addr,
552908fe 155 s->io_buffer + s->io_buffer_index, l);
40a6238a 156 } else {
f6c11d56 157 pci_dma_read(pci_dev, bm->cur_prd_addr,
552908fe 158 s->io_buffer + s->io_buffer_index, l);
40a6238a
AG
159 }
160 bm->cur_prd_addr += l;
161 bm->cur_prd_len -= l;
162 s->io_buffer_index += l;
163 }
164 }
165 return 1;
166}
167
0e7ce54c 168static void bmdma_set_inactive(IDEDMA *dma, bool more)
40a6238a
AG
169{
170 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
40a6238a 171
40a6238a 172 bm->dma_cb = NULL;
0e7ce54c
PB
173 if (more) {
174 bm->status |= BM_STATUS_DMAING;
175 } else {
176 bm->status &= ~BM_STATUS_DMAING;
177 }
40a6238a
AG
178}
179
bd8892c4 180static void bmdma_restart_dma(IDEDMA *dma)
40a6238a 181{
40a6238a
AG
182 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
183
06b95b1e 184 bm->cur_addr = bm->addr;
40a6238a
AG
185}
186
187static void bmdma_cancel(BMDMAState *bm)
188{
189 if (bm->status & BM_STATUS_DMAING) {
190 /* cancel DMA request */
0e7ce54c 191 bmdma_set_inactive(&bm->dma, false);
40a6238a
AG
192 }
193}
194
1374bec0 195static void bmdma_reset(IDEDMA *dma)
40a6238a
AG
196{
197 BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
198
3eee2611 199 trace_bmdma_reset();
40a6238a
AG
200 bmdma_cancel(bm);
201 bm->cmd = 0;
202 bm->status = 0;
203 bm->addr = 0;
204 bm->cur_addr = 0;
205 bm->cur_prd_last = 0;
206 bm->cur_prd_addr = 0;
207 bm->cur_prd_len = 0;
40a6238a
AG
208}
209
40a6238a
AG
210static void bmdma_irq(void *opaque, int n, int level)
211{
212 BMDMAState *bm = opaque;
213
214 if (!level) {
215 /* pass through lower */
216 qemu_set_irq(bm->irq, level);
217 return;
218 }
219
1635eecc 220 bm->status |= BM_STATUS_INT;
40a6238a
AG
221
222 /* trigger the real irq */
223 qemu_set_irq(bm->irq, level);
224}
225
a9deb8c6 226void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)
977e1244 227{
3eee2611 228 trace_bmdma_cmd_writeb(val);
c29947bb
KW
229
230 /* Ignore writes to SSBM if it keeps the old value */
231 if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
232 if (!(val & BM_CMD_START)) {
86698a12 233 ide_cancel_dma_sync(idebus_active_if(bm->bus));
b39f9612 234 bm->status &= ~BM_STATUS_DMAING;
c29947bb 235 } else {
b76876e6 236 bm->cur_addr = bm->addr;
c29947bb
KW
237 if (!(bm->status & BM_STATUS_DMAING)) {
238 bm->status |= BM_STATUS_DMAING;
239 /* start dma transfer if possible */
240 if (bm->dma_cb)
40a6238a 241 bm->dma_cb(bmdma_active_if(bm), 0);
c29947bb 242 }
953844d1 243 }
977e1244 244 }
c29947bb
KW
245
246 bm->cmd = val & 0x09;
977e1244
GH
247}
248
a8170e5e 249static uint64_t bmdma_addr_read(void *opaque, hwaddr addr,
a9deb8c6 250 unsigned width)
977e1244 251{
a9deb8c6 252 BMDMAState *bm = opaque;
9fbef1ac 253 uint32_t mask = (1ULL << (width * 8)) - 1;
a9deb8c6 254 uint64_t data;
977e1244 255
a9deb8c6 256 data = (bm->addr >> (addr * 8)) & mask;
3eee2611 257 trace_bmdma_addr_read(data);
a9deb8c6 258 return data;
977e1244
GH
259}
260
a8170e5e 261static void bmdma_addr_write(void *opaque, hwaddr addr,
a9deb8c6 262 uint64_t data, unsigned width)
977e1244 263{
a9deb8c6 264 BMDMAState *bm = opaque;
9fbef1ac
AK
265 int shift = addr * 8;
266 uint32_t mask = (1ULL << (width * 8)) - 1;
977e1244 267
3eee2611 268 trace_bmdma_addr_write(data);
9fbef1ac
AK
269 bm->addr &= ~(mask << shift);
270 bm->addr |= ((data & mask) << shift) & ~3;
977e1244
GH
271}
272
a9deb8c6 273MemoryRegionOps bmdma_addr_ioport_ops = {
9fbef1ac
AK
274 .read = bmdma_addr_read,
275 .write = bmdma_addr_write,
a9deb8c6 276 .endianness = DEVICE_LITTLE_ENDIAN,
9fbef1ac 277};
977e1244 278
5ee84c33
JQ
279static bool ide_bmdma_current_needed(void *opaque)
280{
281 BMDMAState *bm = opaque;
282
283 return (bm->cur_prd_len != 0);
284}
285
def93791
KW
286static bool ide_bmdma_status_needed(void *opaque)
287{
288 BMDMAState *bm = opaque;
289
290 /* Older versions abused some bits in the status register for internal
291 * error state. If any of these bits are set, we must add a subsection to
292 * transfer the real status register */
293 uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
294
295 return ((bm->status & abused_bits) != 0);
296}
297
44b1ff31 298static int ide_bmdma_pre_save(void *opaque)
def93791
KW
299{
300 BMDMAState *bm = opaque;
301 uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
302
218fd37c
PB
303 if (!(bm->status & BM_STATUS_DMAING) && bm->dma_cb) {
304 bm->bus->error_status =
305 ide_dma_cmd_to_retry(bmdma_active_if(bm)->dma_cmd);
306 }
a96cb236 307 bm->migration_retry_unit = bm->bus->retry_unit;
dc5d0af4
PB
308 bm->migration_retry_sector_num = bm->bus->retry_sector_num;
309 bm->migration_retry_nsector = bm->bus->retry_nsector;
def93791
KW
310 bm->migration_compat_status =
311 (bm->status & ~abused_bits) | (bm->bus->error_status & abused_bits);
44b1ff31
DDAG
312
313 return 0;
def93791
KW
314}
315
316/* This function accesses bm->bus->error_status which is loaded only after
317 * BMDMA itself. This is why the function is called from ide_pci_post_load
318 * instead of being registered with VMState where it would run too early. */
319static int ide_bmdma_post_load(void *opaque, int version_id)
320{
321 BMDMAState *bm = opaque;
322 uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
323
324 if (bm->status == 0) {
325 bm->status = bm->migration_compat_status & ~abused_bits;
326 bm->bus->error_status |= bm->migration_compat_status & abused_bits;
327 }
a96cb236 328 if (bm->bus->error_status) {
dc5d0af4
PB
329 bm->bus->retry_sector_num = bm->migration_retry_sector_num;
330 bm->bus->retry_nsector = bm->migration_retry_nsector;
a96cb236
PB
331 bm->bus->retry_unit = bm->migration_retry_unit;
332 }
def93791
KW
333
334 return 0;
335}
336
5ee84c33
JQ
337static const VMStateDescription vmstate_bmdma_current = {
338 .name = "ide bmdma_current",
339 .version_id = 1,
340 .minimum_version_id = 1,
5cd8cada 341 .needed = ide_bmdma_current_needed,
d49805ae 342 .fields = (VMStateField[]) {
5ee84c33
JQ
343 VMSTATE_UINT32(cur_addr, BMDMAState),
344 VMSTATE_UINT32(cur_prd_last, BMDMAState),
345 VMSTATE_UINT32(cur_prd_addr, BMDMAState),
346 VMSTATE_UINT32(cur_prd_len, BMDMAState),
347 VMSTATE_END_OF_LIST()
348 }
349};
350
06ab66cf 351static const VMStateDescription vmstate_bmdma_status = {
def93791
KW
352 .name ="ide bmdma/status",
353 .version_id = 1,
354 .minimum_version_id = 1,
5cd8cada 355 .needed = ide_bmdma_status_needed,
d49805ae 356 .fields = (VMStateField[]) {
def93791
KW
357 VMSTATE_UINT8(status, BMDMAState),
358 VMSTATE_END_OF_LIST()
359 }
360};
5ee84c33 361
407a4f30
JQ
362static const VMStateDescription vmstate_bmdma = {
363 .name = "ide bmdma",
57338424 364 .version_id = 3,
407a4f30 365 .minimum_version_id = 0,
def93791 366 .pre_save = ide_bmdma_pre_save,
d49805ae 367 .fields = (VMStateField[]) {
407a4f30 368 VMSTATE_UINT8(cmd, BMDMAState),
def93791 369 VMSTATE_UINT8(migration_compat_status, BMDMAState),
407a4f30 370 VMSTATE_UINT32(addr, BMDMAState),
dc5d0af4
PB
371 VMSTATE_INT64(migration_retry_sector_num, BMDMAState),
372 VMSTATE_UINT32(migration_retry_nsector, BMDMAState),
a96cb236 373 VMSTATE_UINT8(migration_retry_unit, BMDMAState),
407a4f30 374 VMSTATE_END_OF_LIST()
5ee84c33 375 },
5cd8cada
JQ
376 .subsections = (const VMStateDescription*[]) {
377 &vmstate_bmdma_current,
378 &vmstate_bmdma_status,
379 NULL
977e1244 380 }
407a4f30 381};
977e1244 382
407a4f30 383static int ide_pci_post_load(void *opaque, int version_id)
977e1244
GH
384{
385 PCIIDEState *d = opaque;
407a4f30 386 int i;
977e1244 387
977e1244 388 for(i = 0; i < 2; i++) {
407a4f30
JQ
389 /* current versions always store 0/1, but older version
390 stored bigger values. We only need last bit */
a96cb236 391 d->bmdma[i].migration_retry_unit &= 1;
def93791 392 ide_bmdma_post_load(&d->bmdma[i], -1);
977e1244 393 }
def93791 394
977e1244
GH
395 return 0;
396}
397
407a4f30
JQ
398const VMStateDescription vmstate_ide_pci = {
399 .name = "ide",
57338424 400 .version_id = 3,
407a4f30 401 .minimum_version_id = 0,
407a4f30 402 .post_load = ide_pci_post_load,
d49805ae 403 .fields = (VMStateField[]) {
f6c11d56 404 VMSTATE_PCI_DEVICE(parent_obj, PCIIDEState),
407a4f30
JQ
405 VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
406 vmstate_bmdma, BMDMAState),
407 VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
408 VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
409 VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
410 VMSTATE_END_OF_LIST()
411 }
412};
413
3e7e1558 414void pci_ide_create_devs(PCIDevice *dev, DriveInfo **hd_table)
feef3102 415{
f6c11d56 416 PCIIDEState *d = PCI_IDE(dev);
feef3102
GH
417 static const int bus[4] = { 0, 0, 1, 1 };
418 static const int unit[4] = { 0, 1, 0, 1 };
419 int i;
420
421 for (i = 0; i < 4; i++) {
422 if (hd_table[i] == NULL)
423 continue;
1f850f10 424 ide_create_drive(d->bus+bus[i], unit[i], hd_table[i]);
feef3102
GH
425 }
426}
40a6238a
AG
427
428static const struct IDEDMAOps bmdma_ops = {
429 .start_dma = bmdma_start_dma,
40a6238a
AG
430 .prepare_buf = bmdma_prepare_buf,
431 .rw_buf = bmdma_rw_buf,
bd8892c4 432 .restart_dma = bmdma_restart_dma,
40a6238a 433 .set_inactive = bmdma_set_inactive,
40a6238a
AG
434 .reset = bmdma_reset,
435};
436
a9deb8c6 437void bmdma_init(IDEBus *bus, BMDMAState *bm, PCIIDEState *d)
40a6238a 438{
40a6238a
AG
439 if (bus->dma == &bm->dma) {
440 return;
441 }
442
443 bm->dma.ops = &bmdma_ops;
444 bus->dma = &bm->dma;
445 bm->irq = bus->irq;
6e38a4ba 446 bus->irq = qemu_allocate_irq(bmdma_irq, bm, 0);
a9deb8c6 447 bm->pci_dev = d;
40a6238a 448}
f6c11d56
AF
449
450static const TypeInfo pci_ide_type_info = {
451 .name = TYPE_PCI_IDE,
452 .parent = TYPE_PCI_DEVICE,
453 .instance_size = sizeof(PCIIDEState),
454 .abstract = true,
fd3b02c8
EH
455 .interfaces = (InterfaceInfo[]) {
456 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
457 { },
458 },
f6c11d56
AF
459};
460
461static void pci_ide_register_types(void)
462{
463 type_register_static(&pci_ide_type_info);
464}
465
466type_init(pci_ide_register_types)
This page took 0.715037 seconds and 4 git commands to generate.