1// Copyright 2018 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <ddk/binding.h> 6#include <ddk/debug.h> 7#include <ddk/device.h> 8#include <ddk/driver.h> 9#include <ddk/protocol/platform-defs.h> 10#include <ddk/protocol/platform-device.h> 11 12#include "../qemu-virt.h" 13 14#define DRIVER_NAME "qemu-test-parent" 15 16typedef struct { 17 zx_device_t* zxdev; 18} qemu_test_t; 19 20static void qemu_test_release(void* ctx) { 21 free(ctx); 22} 23 24static zx_protocol_device_t qemu_test_device_protocol = { 25 .version = DEVICE_OPS_VERSION, 26 .release = qemu_test_release, 27}; 28 29static zx_status_t qemu_test_bti(platform_device_protocol_t* pdev) { 30 zx_status_t status; 31 zx_handle_t bti; 32 33 status = pdev_get_bti(pdev, 0, &bti); 34 if (status == ZX_OK) { 35 // Parent doesn't have any BTIs so this call should have failed. 36 zxlogf(ERROR, "%s: parent got btis it doesn't own!\n", DRIVER_NAME); 37 zx_handle_close(bti); 38 return ZX_ERR_INTERNAL; 39 } 40 41 // Treat any error as a success. 42 return ZX_OK; 43} 44 45static zx_status_t qemu_test_bind(void* ctx, zx_device_t* parent) { 46 platform_device_protocol_t pdev; 47 zx_status_t status; 48 49 zxlogf(INFO, "qemu_test_bind: %s \n", DRIVER_NAME); 50 51 status = device_get_protocol(parent, ZX_PROTOCOL_PLATFORM_DEV, &pdev); 52 if (status != ZX_OK) { 53 zxlogf(ERROR, "%s: could not get ZX_PROTOCOL_PLATFORM_DEV\n", DRIVER_NAME); 54 return status; 55 } 56 57 // Make sure we can access our MMIO. 58 mmio_buffer_t mmio; 59 status = pdev_map_mmio_buffer2(&pdev, 0, ZX_CACHE_POLICY_UNCACHED_DEVICE, &mmio); 60 if (status != ZX_OK) { 61 zxlogf(ERROR, "%s: pdev_map_mmio_buffer failed\n", DRIVER_NAME); 62 return status; 63 } 64 if (mmio.size != TEST_MMIO_1_SIZE) { 65 zxlogf(ERROR, "%s: mmio.size expected %u got %zu\n", DRIVER_NAME, TEST_MMIO_1_SIZE, 66 mmio.size); 67 } 68 mmio_buffer_release(&mmio); 69 70 status = qemu_test_bti(&pdev); 71 if (status != ZX_OK) { 72 zxlogf(ERROR, "%s: bti test failed, st = %d\n", DRIVER_NAME, status); 73 } 74 75 qemu_test_t* test = calloc(1, sizeof(qemu_test_t)); 76 if (!test) { 77 return ZX_ERR_NO_MEMORY; 78 } 79 80 zx_device_prop_t child_props[] = { 81 { BIND_PROTOCOL, 0, ZX_PROTOCOL_PLATFORM_DEV }, 82 { BIND_PLATFORM_DEV_VID, 0, PDEV_VID_QEMU }, 83 { BIND_PLATFORM_DEV_PID, 0, PDEV_PID_QEMU }, 84 { BIND_PLATFORM_DEV_DID, 0, PDEV_DID_QEMU_TEST_CHILD_1 }, 85 }; 86 87 device_add_args_t child_args = { 88 .version = DEVICE_ADD_ARGS_VERSION, 89 .name = "child-1", 90 .ctx = test, 91 .ops = &qemu_test_device_protocol, 92 .props = child_props, 93 .prop_count = countof(child_props), 94 }; 95 96 status = pdev_device_add(&pdev, 0, &child_args, &test->zxdev); 97 if (status != ZX_OK) { 98 zxlogf(ERROR, "%s: pdev_device_add failed: %d\n", DRIVER_NAME, status); 99 free(test); 100 return status; 101 } 102 103 return ZX_OK; 104} 105 106static zx_driver_ops_t qemu_test_driver_ops = { 107 .version = DRIVER_OPS_VERSION, 108 .bind = qemu_test_bind, 109}; 110 111ZIRCON_DRIVER_BEGIN(qemu_bus, qemu_test_driver_ops, "zircon", "0.1", 4) 112 BI_ABORT_IF(NE, BIND_PROTOCOL, ZX_PROTOCOL_PLATFORM_DEV), 113 BI_ABORT_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_QEMU), 114 BI_ABORT_IF(NE, BIND_PLATFORM_DEV_PID, PDEV_PID_QEMU), 115 BI_MATCH_IF(EQ, BIND_PLATFORM_DEV_DID, PDEV_DID_QEMU_TEST_PARENT), 116ZIRCON_DRIVER_END(qemu_bus) 117