linux/drivers/gpu/drm/nouveau/nvkm/core/client.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 Red Hat Inc.
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice shall be included in
  12 * all copies or substantial portions of the Software.
  13 *
  14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20 * OTHER DEALINGS IN THE SOFTWARE.
  21 *
  22 * Authors: Ben Skeggs
  23 */
  24#include <core/client.h>
  25#include <core/device.h>
  26#include <core/notify.h>
  27#include <core/option.h>
  28
  29#include <nvif/class.h>
  30#include <nvif/event.h>
  31#include <nvif/unpack.h>
  32
  33struct nvkm_client_notify {
  34        struct nvkm_client *client;
  35        struct nvkm_notify n;
  36        u8 version;
  37        u8 size;
  38        union {
  39                struct nvif_notify_rep_v0 v0;
  40        } rep;
  41};
  42
  43static int
  44nvkm_client_notify(struct nvkm_notify *n)
  45{
  46        struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
  47        struct nvkm_client *client = notify->client;
  48        return client->ntfy(&notify->rep, notify->size, n->data, n->size);
  49}
  50
  51int
  52nvkm_client_notify_put(struct nvkm_client *client, int index)
  53{
  54        if (index < ARRAY_SIZE(client->notify)) {
  55                if (client->notify[index]) {
  56                        nvkm_notify_put(&client->notify[index]->n);
  57                        return 0;
  58                }
  59        }
  60        return -ENOENT;
  61}
  62
  63int
  64nvkm_client_notify_get(struct nvkm_client *client, int index)
  65{
  66        if (index < ARRAY_SIZE(client->notify)) {
  67                if (client->notify[index]) {
  68                        nvkm_notify_get(&client->notify[index]->n);
  69                        return 0;
  70                }
  71        }
  72        return -ENOENT;
  73}
  74
  75int
  76nvkm_client_notify_del(struct nvkm_client *client, int index)
  77{
  78        if (index < ARRAY_SIZE(client->notify)) {
  79                if (client->notify[index]) {
  80                        nvkm_notify_fini(&client->notify[index]->n);
  81                        kfree(client->notify[index]);
  82                        client->notify[index] = NULL;
  83                        return 0;
  84                }
  85        }
  86        return -ENOENT;
  87}
  88
  89int
  90nvkm_client_notify_new(struct nvkm_object *object,
  91                       struct nvkm_event *event, void *data, u32 size)
  92{
  93        struct nvkm_client *client = object->client;
  94        struct nvkm_client_notify *notify;
  95        union {
  96                struct nvif_notify_req_v0 v0;
  97        } *req = data;
  98        u8  index, reply;
  99        int ret;
 100
 101        for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
 102                if (!client->notify[index])
 103                        break;
 104        }
 105
 106        if (index == ARRAY_SIZE(client->notify))
 107                return -ENOSPC;
 108
 109        notify = kzalloc(sizeof(*notify), GFP_KERNEL);
 110        if (!notify)
 111                return -ENOMEM;
 112
 113        nvif_ioctl(object, "notify new size %d\n", size);
 114        if (nvif_unpack(req->v0, 0, 0, true)) {
 115                nvif_ioctl(object, "notify new vers %d reply %d route %02x "
 116                                   "token %llx\n", req->v0.version,
 117                           req->v0.reply, req->v0.route, req->v0.token);
 118                notify->version = req->v0.version;
 119                notify->size = sizeof(notify->rep.v0);
 120                notify->rep.v0.version = req->v0.version;
 121                notify->rep.v0.route = req->v0.route;
 122                notify->rep.v0.token = req->v0.token;
 123                reply = req->v0.reply;
 124        }
 125
 126        if (ret == 0) {
 127                ret = nvkm_notify_init(object, event, nvkm_client_notify,
 128                                       false, data, size, reply, &notify->n);
 129                if (ret == 0) {
 130                        client->notify[index] = notify;
 131                        notify->client = client;
 132                        return index;
 133                }
 134        }
 135
 136        kfree(notify);
 137        return ret;
 138}
 139
 140static int
 141nvkm_client_mthd_devlist(struct nvkm_object *object, void *data, u32 size)
 142{
 143        union {
 144                struct nv_client_devlist_v0 v0;
 145        } *args = data;
 146        int ret;
 147
 148        nvif_ioctl(object, "client devlist size %d\n", size);
 149        if (nvif_unpack(args->v0, 0, 0, true)) {
 150                nvif_ioctl(object, "client devlist vers %d count %d\n",
 151                           args->v0.version, args->v0.count);
 152                if (size == sizeof(args->v0.device[0]) * args->v0.count) {
 153                        ret = nvkm_device_list(args->v0.device, args->v0.count);
 154                        if (ret >= 0) {
 155                                args->v0.count = ret;
 156                                ret = 0;
 157                        }
 158                } else {
 159                        ret = -EINVAL;
 160                }
 161        }
 162
 163        return ret;
 164}
 165
 166static int
 167nvkm_client_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
 168{
 169        switch (mthd) {
 170        case NV_CLIENT_DEVLIST:
 171                return nvkm_client_mthd_devlist(object, data, size);
 172        default:
 173                break;
 174        }
 175        return -EINVAL;
 176}
 177
 178static int
 179nvkm_client_child_new(const struct nvkm_oclass *oclass,
 180                      void *data, u32 size, struct nvkm_object **pobject)
 181{
 182        return oclass->base.ctor(oclass, data, size, pobject);
 183}
 184
 185static int
 186nvkm_client_child_get(struct nvkm_object *object, int index,
 187                      struct nvkm_oclass *oclass)
 188{
 189        const struct nvkm_sclass *sclass;
 190
 191        switch (index) {
 192        case 0: sclass = &nvkm_udevice_sclass; break;
 193        default:
 194                return -EINVAL;
 195        }
 196
 197        oclass->ctor = nvkm_client_child_new;
 198        oclass->base = *sclass;
 199        return 0;
 200}
 201
 202static const struct nvkm_object_func
 203nvkm_client_object_func = {
 204        .mthd = nvkm_client_mthd,
 205        .sclass = nvkm_client_child_get,
 206};
 207
 208void
 209nvkm_client_remove(struct nvkm_client *client, struct nvkm_object *object)
 210{
 211        if (!RB_EMPTY_NODE(&object->node))
 212                rb_erase(&object->node, &client->objroot);
 213}
 214
 215bool
 216nvkm_client_insert(struct nvkm_client *client, struct nvkm_object *object)
 217{
 218        struct rb_node **ptr = &client->objroot.rb_node;
 219        struct rb_node *parent = NULL;
 220
 221        while (*ptr) {
 222                struct nvkm_object *this =
 223                        container_of(*ptr, typeof(*this), node);
 224                parent = *ptr;
 225                if (object->object < this->object)
 226                        ptr = &parent->rb_left;
 227                else
 228                if (object->object > this->object)
 229                        ptr = &parent->rb_right;
 230                else
 231                        return false;
 232        }
 233
 234        rb_link_node(&object->node, parent, ptr);
 235        rb_insert_color(&object->node, &client->objroot);
 236        return true;
 237}
 238
 239struct nvkm_object *
 240nvkm_client_search(struct nvkm_client *client, u64 handle)
 241{
 242        struct rb_node *node = client->objroot.rb_node;
 243        while (node) {
 244                struct nvkm_object *object =
 245                        container_of(node, typeof(*object), node);
 246                if (handle < object->object)
 247                        node = node->rb_left;
 248                else
 249                if (handle > object->object)
 250                        node = node->rb_right;
 251                else
 252                        return object;
 253        }
 254        return NULL;
 255}
 256
 257int
 258nvkm_client_fini(struct nvkm_client *client, bool suspend)
 259{
 260        struct nvkm_object *object = &client->object;
 261        const char *name[2] = { "fini", "suspend" };
 262        int i;
 263        nvif_debug(object, "%s notify\n", name[suspend]);
 264        for (i = 0; i < ARRAY_SIZE(client->notify); i++)
 265                nvkm_client_notify_put(client, i);
 266        return nvkm_object_fini(&client->object, suspend);
 267}
 268
 269int
 270nvkm_client_init(struct nvkm_client *client)
 271{
 272        return nvkm_object_init(&client->object);
 273}
 274
 275void
 276nvkm_client_del(struct nvkm_client **pclient)
 277{
 278        struct nvkm_client *client = *pclient;
 279        int i;
 280        if (client) {
 281                nvkm_client_fini(client, false);
 282                for (i = 0; i < ARRAY_SIZE(client->notify); i++)
 283                        nvkm_client_notify_del(client, i);
 284                nvkm_object_dtor(&client->object);
 285                kfree(*pclient);
 286                *pclient = NULL;
 287        }
 288}
 289
 290int
 291nvkm_client_new(const char *name, u64 device, const char *cfg,
 292                const char *dbg, struct nvkm_client **pclient)
 293{
 294        struct nvkm_oclass oclass = {};
 295        struct nvkm_client *client;
 296
 297        if (!(client = *pclient = kzalloc(sizeof(*client), GFP_KERNEL)))
 298                return -ENOMEM;
 299        oclass.client = client;
 300
 301        nvkm_object_ctor(&nvkm_client_object_func, &oclass, &client->object);
 302        snprintf(client->name, sizeof(client->name), "%s", name);
 303        client->device = device;
 304        client->debug = nvkm_dbgopt(dbg, "CLIENT");
 305        client->objroot = RB_ROOT;
 306        client->dmaroot = RB_ROOT;
 307        return 0;
 308}
 309