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/if0000.h>
  32#include <nvif/unpack.h>
  33
  34static int
  35nvkm_uclient_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
  36                 struct nvkm_object **pobject)
  37{
  38        union {
  39                struct nvif_client_v0 v0;
  40        } *args = argv;
  41        struct nvkm_client *client;
  42        int ret = -ENOSYS;
  43
  44        if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))){
  45                args->v0.name[sizeof(args->v0.name) - 1] = 0;
  46                ret = nvkm_client_new(args->v0.name, args->v0.device, NULL,
  47                                      NULL, oclass->client->ntfy, &client);
  48                if (ret)
  49                        return ret;
  50        } else
  51                return ret;
  52
  53        client->object.client = oclass->client;
  54        client->object.handle = oclass->handle;
  55        client->object.route  = oclass->route;
  56        client->object.token  = oclass->token;
  57        client->object.object = oclass->object;
  58        client->debug = oclass->client->debug;
  59        *pobject = &client->object;
  60        return 0;
  61}
  62
  63const struct nvkm_sclass
  64nvkm_uclient_sclass = {
  65        .oclass = NVIF_CLASS_CLIENT,
  66        .minver = 0,
  67        .maxver = 0,
  68        .ctor = nvkm_uclient_new,
  69};
  70
  71struct nvkm_client_notify {
  72        struct nvkm_client *client;
  73        struct nvkm_notify n;
  74        u8 version;
  75        u8 size;
  76        union {
  77                struct nvif_notify_rep_v0 v0;
  78        } rep;
  79};
  80
  81static int
  82nvkm_client_notify(struct nvkm_notify *n)
  83{
  84        struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
  85        struct nvkm_client *client = notify->client;
  86        return client->ntfy(&notify->rep, notify->size, n->data, n->size);
  87}
  88
  89int
  90nvkm_client_notify_put(struct nvkm_client *client, int index)
  91{
  92        if (index < ARRAY_SIZE(client->notify)) {
  93                if (client->notify[index]) {
  94                        nvkm_notify_put(&client->notify[index]->n);
  95                        return 0;
  96                }
  97        }
  98        return -ENOENT;
  99}
 100
 101int
 102nvkm_client_notify_get(struct nvkm_client *client, int index)
 103{
 104        if (index < ARRAY_SIZE(client->notify)) {
 105                if (client->notify[index]) {
 106                        nvkm_notify_get(&client->notify[index]->n);
 107                        return 0;
 108                }
 109        }
 110        return -ENOENT;
 111}
 112
 113int
 114nvkm_client_notify_del(struct nvkm_client *client, int index)
 115{
 116        if (index < ARRAY_SIZE(client->notify)) {
 117                if (client->notify[index]) {
 118                        nvkm_notify_fini(&client->notify[index]->n);
 119                        kfree(client->notify[index]);
 120                        client->notify[index] = NULL;
 121                        return 0;
 122                }
 123        }
 124        return -ENOENT;
 125}
 126
 127int
 128nvkm_client_notify_new(struct nvkm_object *object,
 129                       struct nvkm_event *event, void *data, u32 size)
 130{
 131        struct nvkm_client *client = object->client;
 132        struct nvkm_client_notify *notify;
 133        union {
 134                struct nvif_notify_req_v0 v0;
 135        } *req = data;
 136        u8  index, reply;
 137        int ret = -ENOSYS;
 138
 139        for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
 140                if (!client->notify[index])
 141                        break;
 142        }
 143
 144        if (index == ARRAY_SIZE(client->notify))
 145                return -ENOSPC;
 146
 147        notify = kzalloc(sizeof(*notify), GFP_KERNEL);
 148        if (!notify)
 149                return -ENOMEM;
 150
 151        nvif_ioctl(object, "notify new size %d\n", size);
 152        if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
 153                nvif_ioctl(object, "notify new vers %d reply %d route %02x "
 154                                   "token %llx\n", req->v0.version,
 155                           req->v0.reply, req->v0.route, req->v0.token);
 156                notify->version = req->v0.version;
 157                notify->size = sizeof(notify->rep.v0);
 158                notify->rep.v0.version = req->v0.version;
 159                notify->rep.v0.route = req->v0.route;
 160                notify->rep.v0.token = req->v0.token;
 161                reply = req->v0.reply;
 162        }
 163
 164        if (ret == 0) {
 165                ret = nvkm_notify_init(object, event, nvkm_client_notify,
 166                                       false, data, size, reply, &notify->n);
 167                if (ret == 0) {
 168                        client->notify[index] = notify;
 169                        notify->client = client;
 170                        return index;
 171                }
 172        }
 173
 174        kfree(notify);
 175        return ret;
 176}
 177
 178static const struct nvkm_object_func nvkm_client;
 179struct nvkm_client *
 180nvkm_client_search(struct nvkm_client *client, u64 handle)
 181{
 182        struct nvkm_object *object;
 183
 184        object = nvkm_object_search(client, handle, &nvkm_client);
 185        if (IS_ERR(object))
 186                return (void *)object;
 187
 188        return nvkm_client(object);
 189}
 190
 191static int
 192nvkm_client_mthd_devlist(struct nvkm_client *client, void *data, u32 size)
 193{
 194        union {
 195                struct nvif_client_devlist_v0 v0;
 196        } *args = data;
 197        int ret = -ENOSYS;
 198
 199        nvif_ioctl(&client->object, "client devlist size %d\n", size);
 200        if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
 201                nvif_ioctl(&client->object, "client devlist vers %d count %d\n",
 202                           args->v0.version, args->v0.count);
 203                if (size == sizeof(args->v0.device[0]) * args->v0.count) {
 204                        ret = nvkm_device_list(args->v0.device, args->v0.count);
 205                        if (ret >= 0) {
 206                                args->v0.count = ret;
 207                                ret = 0;
 208                        }
 209                } else {
 210                        ret = -EINVAL;
 211                }
 212        }
 213
 214        return ret;
 215}
 216
 217static int
 218nvkm_client_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
 219{
 220        struct nvkm_client *client = nvkm_client(object);
 221        switch (mthd) {
 222        case NVIF_CLIENT_V0_DEVLIST:
 223                return nvkm_client_mthd_devlist(client, data, size);
 224        default:
 225                break;
 226        }
 227        return -EINVAL;
 228}
 229
 230static int
 231nvkm_client_child_new(const struct nvkm_oclass *oclass,
 232                      void *data, u32 size, struct nvkm_object **pobject)
 233{
 234        return oclass->base.ctor(oclass, data, size, pobject);
 235}
 236
 237static int
 238nvkm_client_child_get(struct nvkm_object *object, int index,
 239                      struct nvkm_oclass *oclass)
 240{
 241        const struct nvkm_sclass *sclass;
 242
 243        switch (index) {
 244        case 0: sclass = &nvkm_uclient_sclass; break;
 245        case 1: sclass = &nvkm_udevice_sclass; break;
 246        default:
 247                return -EINVAL;
 248        }
 249
 250        oclass->ctor = nvkm_client_child_new;
 251        oclass->base = *sclass;
 252        return 0;
 253}
 254
 255static int
 256nvkm_client_fini(struct nvkm_object *object, bool suspend)
 257{
 258        struct nvkm_client *client = nvkm_client(object);
 259        const char *name[2] = { "fini", "suspend" };
 260        int i;
 261        nvif_debug(object, "%s notify\n", name[suspend]);
 262        for (i = 0; i < ARRAY_SIZE(client->notify); i++)
 263                nvkm_client_notify_put(client, i);
 264        return 0;
 265}
 266
 267static void *
 268nvkm_client_dtor(struct nvkm_object *object)
 269{
 270        struct nvkm_client *client = nvkm_client(object);
 271        int i;
 272        for (i = 0; i < ARRAY_SIZE(client->notify); i++)
 273                nvkm_client_notify_del(client, i);
 274        return client;
 275}
 276
 277static const struct nvkm_object_func
 278nvkm_client = {
 279        .dtor = nvkm_client_dtor,
 280        .fini = nvkm_client_fini,
 281        .mthd = nvkm_client_mthd,
 282        .sclass = nvkm_client_child_get,
 283};
 284
 285int
 286nvkm_client_new(const char *name, u64 device, const char *cfg,
 287                const char *dbg,
 288                int (*ntfy)(const void *, u32, const void *, u32),
 289                struct nvkm_client **pclient)
 290{
 291        struct nvkm_oclass oclass = { .base = nvkm_uclient_sclass };
 292        struct nvkm_client *client;
 293
 294        if (!(client = *pclient = kzalloc(sizeof(*client), GFP_KERNEL)))
 295                return -ENOMEM;
 296        oclass.client = client;
 297
 298        nvkm_object_ctor(&nvkm_client, &oclass, &client->object);
 299        snprintf(client->name, sizeof(client->name), "%s", name);
 300        client->device = device;
 301        client->debug = nvkm_dbgopt(dbg, "CLIENT");
 302        client->objroot = RB_ROOT;
 303        client->ntfy = ntfy;
 304        INIT_LIST_HEAD(&client->umem);
 305        spin_lock_init(&client->lock);
 306        return 0;
 307}
 308