xref: /openbmc/qemu/hw/ide/pci.c (revision 073d9f2c)
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  */
25 #include "qemu/osdep.h"
26 #include "hw/hw.h"
27 #include "hw/pci/pci.h"
28 #include "sysemu/dma.h"
29 #include "qemu/error-report.h"
30 #include "hw/ide/pci.h"
31 #include "trace.h"
32 
33 #define BMDMA_PAGE_SIZE 4096
34 
35 #define BM_MIGRATION_COMPAT_STATUS_BITS \
36         (IDE_RETRY_DMA | IDE_RETRY_PIO | \
37         IDE_RETRY_READ | IDE_RETRY_FLUSH)
38 
39 static void bmdma_start_dma(IDEDMA *dma, IDEState *s,
40                             BlockCompletionFunc *dma_cb)
41 {
42     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
43 
44     bm->dma_cb = dma_cb;
45     bm->cur_prd_last = 0;
46     bm->cur_prd_addr = 0;
47     bm->cur_prd_len = 0;
48 
49     if (bm->status & BM_STATUS_DMAING) {
50         bm->dma_cb(bmdma_active_if(bm), 0);
51     }
52 }
53 
54 /**
55  * Prepare an sglist based on available PRDs.
56  * @limit: How many bytes to prepare total.
57  *
58  * Returns the number of bytes prepared, -1 on error.
59  * IDEState.io_buffer_size will contain the number of bytes described
60  * by the PRDs, whether or not we added them to the sglist.
61  */
62 static int32_t bmdma_prepare_buf(IDEDMA *dma, int32_t limit)
63 {
64     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
65     IDEState *s = bmdma_active_if(bm);
66     PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
67     struct {
68         uint32_t addr;
69         uint32_t size;
70     } prd;
71     int l, len;
72 
73     pci_dma_sglist_init(&s->sg, pci_dev,
74                         s->nsector / (BMDMA_PAGE_SIZE / 512) + 1);
75     s->io_buffer_size = 0;
76     for(;;) {
77         if (bm->cur_prd_len == 0) {
78             /* end of table (with a fail safe of one page) */
79             if (bm->cur_prd_last ||
80                 (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
81                 return s->sg.size;
82             }
83             pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
84             bm->cur_addr += 8;
85             prd.addr = le32_to_cpu(prd.addr);
86             prd.size = le32_to_cpu(prd.size);
87             len = prd.size & 0xfffe;
88             if (len == 0)
89                 len = 0x10000;
90             bm->cur_prd_len = len;
91             bm->cur_prd_addr = prd.addr;
92             bm->cur_prd_last = (prd.size & 0x80000000);
93         }
94         l = bm->cur_prd_len;
95         if (l > 0) {
96             uint64_t sg_len;
97 
98             /* Don't add extra bytes to the SGList; consume any remaining
99              * PRDs from the guest, but ignore them. */
100             sg_len = MIN(limit - s->sg.size, bm->cur_prd_len);
101             if (sg_len) {
102                 qemu_sglist_add(&s->sg, bm->cur_prd_addr, sg_len);
103             }
104 
105             bm->cur_prd_addr += l;
106             bm->cur_prd_len -= l;
107             s->io_buffer_size += l;
108         }
109     }
110 
111     qemu_sglist_destroy(&s->sg);
112     s->io_buffer_size = 0;
113     return -1;
114 }
115 
116 /* return 0 if buffer completed */
117 static int bmdma_rw_buf(IDEDMA *dma, int is_write)
118 {
119     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
120     IDEState *s = bmdma_active_if(bm);
121     PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
122     struct {
123         uint32_t addr;
124         uint32_t size;
125     } prd;
126     int l, len;
127 
128     for(;;) {
129         l = s->io_buffer_size - s->io_buffer_index;
130         if (l <= 0)
131             break;
132         if (bm->cur_prd_len == 0) {
133             /* end of table (with a fail safe of one page) */
134             if (bm->cur_prd_last ||
135                 (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE)
136                 return 0;
137             pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
138             bm->cur_addr += 8;
139             prd.addr = le32_to_cpu(prd.addr);
140             prd.size = le32_to_cpu(prd.size);
141             len = prd.size & 0xfffe;
142             if (len == 0)
143                 len = 0x10000;
144             bm->cur_prd_len = len;
145             bm->cur_prd_addr = prd.addr;
146             bm->cur_prd_last = (prd.size & 0x80000000);
147         }
148         if (l > bm->cur_prd_len)
149             l = bm->cur_prd_len;
150         if (l > 0) {
151             if (is_write) {
152                 pci_dma_write(pci_dev, bm->cur_prd_addr,
153                               s->io_buffer + s->io_buffer_index, l);
154             } else {
155                 pci_dma_read(pci_dev, bm->cur_prd_addr,
156                              s->io_buffer + s->io_buffer_index, l);
157             }
158             bm->cur_prd_addr += l;
159             bm->cur_prd_len -= l;
160             s->io_buffer_index += l;
161         }
162     }
163     return 1;
164 }
165 
166 static void bmdma_set_inactive(IDEDMA *dma, bool more)
167 {
168     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
169 
170     bm->dma_cb = NULL;
171     if (more) {
172         bm->status |= BM_STATUS_DMAING;
173     } else {
174         bm->status &= ~BM_STATUS_DMAING;
175     }
176 }
177 
178 static void bmdma_restart_dma(IDEDMA *dma)
179 {
180     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
181 
182     bm->cur_addr = bm->addr;
183 }
184 
185 static void bmdma_cancel(BMDMAState *bm)
186 {
187     if (bm->status & BM_STATUS_DMAING) {
188         /* cancel DMA request */
189         bmdma_set_inactive(&bm->dma, false);
190     }
191 }
192 
193 static void bmdma_reset(IDEDMA *dma)
194 {
195     BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
196 
197     trace_bmdma_reset();
198     bmdma_cancel(bm);
199     bm->cmd = 0;
200     bm->status = 0;
201     bm->addr = 0;
202     bm->cur_addr = 0;
203     bm->cur_prd_last = 0;
204     bm->cur_prd_addr = 0;
205     bm->cur_prd_len = 0;
206 }
207 
208 static void bmdma_irq(void *opaque, int n, int level)
209 {
210     BMDMAState *bm = opaque;
211 
212     if (!level) {
213         /* pass through lower */
214         qemu_set_irq(bm->irq, level);
215         return;
216     }
217 
218     bm->status |= BM_STATUS_INT;
219 
220     /* trigger the real irq */
221     qemu_set_irq(bm->irq, level);
222 }
223 
224 void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)
225 {
226     trace_bmdma_cmd_writeb(val);
227 
228     /* Ignore writes to SSBM if it keeps the old value */
229     if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
230         if (!(val & BM_CMD_START)) {
231             ide_cancel_dma_sync(idebus_active_if(bm->bus));
232             bm->status &= ~BM_STATUS_DMAING;
233         } else {
234             bm->cur_addr = bm->addr;
235             if (!(bm->status & BM_STATUS_DMAING)) {
236                 bm->status |= BM_STATUS_DMAING;
237                 /* start dma transfer if possible */
238                 if (bm->dma_cb)
239                     bm->dma_cb(bmdma_active_if(bm), 0);
240             }
241         }
242     }
243 
244     bm->cmd = val & 0x09;
245 }
246 
247 static uint64_t bmdma_addr_read(void *opaque, hwaddr addr,
248                                 unsigned width)
249 {
250     BMDMAState *bm = opaque;
251     uint32_t mask = (1ULL << (width * 8)) - 1;
252     uint64_t data;
253 
254     data = (bm->addr >> (addr * 8)) & mask;
255     trace_bmdma_addr_read(data);
256     return data;
257 }
258 
259 static void bmdma_addr_write(void *opaque, hwaddr addr,
260                              uint64_t data, unsigned width)
261 {
262     BMDMAState *bm = opaque;
263     int shift = addr * 8;
264     uint32_t mask = (1ULL << (width * 8)) - 1;
265 
266     trace_bmdma_addr_write(data);
267     bm->addr &= ~(mask << shift);
268     bm->addr |= ((data & mask) << shift) & ~3;
269 }
270 
271 MemoryRegionOps bmdma_addr_ioport_ops = {
272     .read = bmdma_addr_read,
273     .write = bmdma_addr_write,
274     .endianness = DEVICE_LITTLE_ENDIAN,
275 };
276 
277 static bool ide_bmdma_current_needed(void *opaque)
278 {
279     BMDMAState *bm = opaque;
280 
281     return (bm->cur_prd_len != 0);
282 }
283 
284 static bool ide_bmdma_status_needed(void *opaque)
285 {
286     BMDMAState *bm = opaque;
287 
288     /* Older versions abused some bits in the status register for internal
289      * error state. If any of these bits are set, we must add a subsection to
290      * transfer the real status register */
291     uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
292 
293     return ((bm->status & abused_bits) != 0);
294 }
295 
296 static int ide_bmdma_pre_save(void *opaque)
297 {
298     BMDMAState *bm = opaque;
299     uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
300 
301     if (!(bm->status & BM_STATUS_DMAING) && bm->dma_cb) {
302         bm->bus->error_status =
303             ide_dma_cmd_to_retry(bmdma_active_if(bm)->dma_cmd);
304     }
305     bm->migration_retry_unit = bm->bus->retry_unit;
306     bm->migration_retry_sector_num = bm->bus->retry_sector_num;
307     bm->migration_retry_nsector = bm->bus->retry_nsector;
308     bm->migration_compat_status =
309         (bm->status & ~abused_bits) | (bm->bus->error_status & abused_bits);
310 
311     return 0;
312 }
313 
314 /* This function accesses bm->bus->error_status which is loaded only after
315  * BMDMA itself. This is why the function is called from ide_pci_post_load
316  * instead of being registered with VMState where it would run too early. */
317 static int ide_bmdma_post_load(void *opaque, int version_id)
318 {
319     BMDMAState *bm = opaque;
320     uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
321 
322     if (bm->status == 0) {
323         bm->status = bm->migration_compat_status & ~abused_bits;
324         bm->bus->error_status |= bm->migration_compat_status & abused_bits;
325     }
326     if (bm->bus->error_status) {
327         bm->bus->retry_sector_num = bm->migration_retry_sector_num;
328         bm->bus->retry_nsector = bm->migration_retry_nsector;
329         bm->bus->retry_unit = bm->migration_retry_unit;
330     }
331 
332     return 0;
333 }
334 
335 static const VMStateDescription vmstate_bmdma_current = {
336     .name = "ide bmdma_current",
337     .version_id = 1,
338     .minimum_version_id = 1,
339     .needed = ide_bmdma_current_needed,
340     .fields = (VMStateField[]) {
341         VMSTATE_UINT32(cur_addr, BMDMAState),
342         VMSTATE_UINT32(cur_prd_last, BMDMAState),
343         VMSTATE_UINT32(cur_prd_addr, BMDMAState),
344         VMSTATE_UINT32(cur_prd_len, BMDMAState),
345         VMSTATE_END_OF_LIST()
346     }
347 };
348 
349 static const VMStateDescription vmstate_bmdma_status = {
350     .name ="ide bmdma/status",
351     .version_id = 1,
352     .minimum_version_id = 1,
353     .needed = ide_bmdma_status_needed,
354     .fields = (VMStateField[]) {
355         VMSTATE_UINT8(status, BMDMAState),
356         VMSTATE_END_OF_LIST()
357     }
358 };
359 
360 static const VMStateDescription vmstate_bmdma = {
361     .name = "ide bmdma",
362     .version_id = 3,
363     .minimum_version_id = 0,
364     .pre_save  = ide_bmdma_pre_save,
365     .fields = (VMStateField[]) {
366         VMSTATE_UINT8(cmd, BMDMAState),
367         VMSTATE_UINT8(migration_compat_status, BMDMAState),
368         VMSTATE_UINT32(addr, BMDMAState),
369         VMSTATE_INT64(migration_retry_sector_num, BMDMAState),
370         VMSTATE_UINT32(migration_retry_nsector, BMDMAState),
371         VMSTATE_UINT8(migration_retry_unit, BMDMAState),
372         VMSTATE_END_OF_LIST()
373     },
374     .subsections = (const VMStateDescription*[]) {
375         &vmstate_bmdma_current,
376         &vmstate_bmdma_status,
377         NULL
378     }
379 };
380 
381 static int ide_pci_post_load(void *opaque, int version_id)
382 {
383     PCIIDEState *d = opaque;
384     int i;
385 
386     for(i = 0; i < 2; i++) {
387         /* current versions always store 0/1, but older version
388            stored bigger values. We only need last bit */
389         d->bmdma[i].migration_retry_unit &= 1;
390         ide_bmdma_post_load(&d->bmdma[i], -1);
391     }
392 
393     return 0;
394 }
395 
396 const VMStateDescription vmstate_ide_pci = {
397     .name = "ide",
398     .version_id = 3,
399     .minimum_version_id = 0,
400     .post_load = ide_pci_post_load,
401     .fields = (VMStateField[]) {
402         VMSTATE_PCI_DEVICE(parent_obj, PCIIDEState),
403         VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
404                              vmstate_bmdma, BMDMAState),
405         VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
406         VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
407         VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
408         VMSTATE_END_OF_LIST()
409     }
410 };
411 
412 void pci_ide_create_devs(PCIDevice *dev, DriveInfo **hd_table)
413 {
414     PCIIDEState *d = PCI_IDE(dev);
415     static const int bus[4]  = { 0, 0, 1, 1 };
416     static const int unit[4] = { 0, 1, 0, 1 };
417     int i;
418 
419     for (i = 0; i < 4; i++) {
420         if (hd_table[i] == NULL)
421             continue;
422         ide_create_drive(d->bus+bus[i], unit[i], hd_table[i]);
423     }
424 }
425 
426 static const struct IDEDMAOps bmdma_ops = {
427     .start_dma = bmdma_start_dma,
428     .prepare_buf = bmdma_prepare_buf,
429     .rw_buf = bmdma_rw_buf,
430     .restart_dma = bmdma_restart_dma,
431     .set_inactive = bmdma_set_inactive,
432     .reset = bmdma_reset,
433 };
434 
435 void bmdma_init(IDEBus *bus, BMDMAState *bm, PCIIDEState *d)
436 {
437     if (bus->dma == &bm->dma) {
438         return;
439     }
440 
441     bm->dma.ops = &bmdma_ops;
442     bus->dma = &bm->dma;
443     bm->irq = bus->irq;
444     bus->irq = qemu_allocate_irq(bmdma_irq, bm, 0);
445     bm->pci_dev = d;
446 }
447 
448 static const TypeInfo pci_ide_type_info = {
449     .name = TYPE_PCI_IDE,
450     .parent = TYPE_PCI_DEVICE,
451     .instance_size = sizeof(PCIIDEState),
452     .abstract = true,
453     .interfaces = (InterfaceInfo[]) {
454         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
455         { },
456     },
457 };
458 
459 static void pci_ide_register_types(void)
460 {
461     type_register_static(&pci_ide_type_info);
462 }
463 
464 type_init(pci_ide_register_types)
465