1 /* 2 * Copyright 2016-17 IBM Corp. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 */ 9 10 #define pr_fmt(fmt) "vas: " fmt 11 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/export.h> 15 #include <linux/types.h> 16 #include <linux/slab.h> 17 #include <linux/platform_device.h> 18 #include <linux/of_platform.h> 19 #include <linux/of_address.h> 20 #include <linux/of.h> 21 22 #include "vas.h" 23 24 static DEFINE_MUTEX(vas_mutex); 25 static LIST_HEAD(vas_instances); 26 27 static int init_vas_instance(struct platform_device *pdev) 28 { 29 int rc, vasid; 30 struct resource *res; 31 struct vas_instance *vinst; 32 struct device_node *dn = pdev->dev.of_node; 33 34 rc = of_property_read_u32(dn, "ibm,vas-id", &vasid); 35 if (rc) { 36 pr_err("No ibm,vas-id property for %s?\n", pdev->name); 37 return -ENODEV; 38 } 39 40 if (pdev->num_resources != 4) { 41 pr_err("Unexpected DT configuration for [%s, %d]\n", 42 pdev->name, vasid); 43 return -ENODEV; 44 } 45 46 vinst = kzalloc(sizeof(*vinst), GFP_KERNEL); 47 if (!vinst) 48 return -ENOMEM; 49 50 INIT_LIST_HEAD(&vinst->node); 51 ida_init(&vinst->ida); 52 mutex_init(&vinst->mutex); 53 vinst->vas_id = vasid; 54 vinst->pdev = pdev; 55 56 res = &pdev->resource[0]; 57 vinst->hvwc_bar_start = res->start; 58 59 res = &pdev->resource[1]; 60 vinst->uwc_bar_start = res->start; 61 62 res = &pdev->resource[2]; 63 vinst->paste_base_addr = res->start; 64 65 res = &pdev->resource[3]; 66 if (res->end > 62) { 67 pr_err("Bad 'paste_win_id_shift' in DT, %llx\n", res->end); 68 goto free_vinst; 69 } 70 71 vinst->paste_win_id_shift = 63 - res->end; 72 73 pr_devel("Initialized instance [%s, %d], paste_base 0x%llx, " 74 "paste_win_id_shift 0x%llx\n", pdev->name, vasid, 75 vinst->paste_base_addr, vinst->paste_win_id_shift); 76 77 mutex_lock(&vas_mutex); 78 list_add(&vinst->node, &vas_instances); 79 mutex_unlock(&vas_mutex); 80 81 dev_set_drvdata(&pdev->dev, vinst); 82 83 return 0; 84 85 free_vinst: 86 kfree(vinst); 87 return -ENODEV; 88 89 } 90 91 /* 92 * Although this is read/used multiple times, it is written to only 93 * during initialization. 94 */ 95 struct vas_instance *find_vas_instance(int vasid) 96 { 97 struct list_head *ent; 98 struct vas_instance *vinst; 99 100 mutex_lock(&vas_mutex); 101 list_for_each(ent, &vas_instances) { 102 vinst = list_entry(ent, struct vas_instance, node); 103 if (vinst->vas_id == vasid) { 104 mutex_unlock(&vas_mutex); 105 return vinst; 106 } 107 } 108 mutex_unlock(&vas_mutex); 109 110 pr_devel("Instance %d not found\n", vasid); 111 return NULL; 112 } 113 114 static int vas_probe(struct platform_device *pdev) 115 { 116 return init_vas_instance(pdev); 117 } 118 119 static const struct of_device_id powernv_vas_match[] = { 120 { .compatible = "ibm,vas",}, 121 {}, 122 }; 123 124 static struct platform_driver vas_driver = { 125 .driver = { 126 .name = "vas", 127 .of_match_table = powernv_vas_match, 128 }, 129 .probe = vas_probe, 130 }; 131 132 static int __init vas_init(void) 133 { 134 int found = 0; 135 struct device_node *dn; 136 137 platform_driver_register(&vas_driver); 138 139 for_each_compatible_node(dn, NULL, "ibm,vas") { 140 of_platform_device_create(dn, NULL, NULL); 141 found++; 142 } 143 144 if (!found) 145 return -ENODEV; 146 147 pr_devel("Found %d instances\n", found); 148 149 return 0; 150 } 151 device_initcall(vas_init); 152