i915_gem.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978
  1. /*
  2. * Copyright © 2008 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/intel-gtt.h>
  37. static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
  38. static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
  39. bool pipelined);
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  42. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  43. int write);
  44. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  45. uint64_t offset,
  46. uint64_t size);
  47. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  48. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
  49. bool interruptible);
  50. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  51. unsigned alignment);
  52. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  53. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  54. struct drm_i915_gem_pwrite *args,
  55. struct drm_file *file_priv);
  56. static void i915_gem_free_object_tail(struct drm_gem_object *obj);
  57. static LIST_HEAD(shrink_list);
  58. static DEFINE_SPINLOCK(shrink_list_lock);
  59. static inline bool
  60. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
  61. {
  62. return obj_priv->gtt_space &&
  63. !obj_priv->active &&
  64. obj_priv->pin_count == 0;
  65. }
  66. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  67. unsigned long end)
  68. {
  69. drm_i915_private_t *dev_priv = dev->dev_private;
  70. if (start >= end ||
  71. (start & (PAGE_SIZE - 1)) != 0 ||
  72. (end & (PAGE_SIZE - 1)) != 0) {
  73. return -EINVAL;
  74. }
  75. drm_mm_init(&dev_priv->mm.gtt_space, start,
  76. end - start);
  77. dev->gtt_total = (uint32_t) (end - start);
  78. return 0;
  79. }
  80. int
  81. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  82. struct drm_file *file_priv)
  83. {
  84. struct drm_i915_gem_init *args = data;
  85. int ret;
  86. mutex_lock(&dev->struct_mutex);
  87. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  88. mutex_unlock(&dev->struct_mutex);
  89. return ret;
  90. }
  91. int
  92. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  93. struct drm_file *file_priv)
  94. {
  95. struct drm_i915_gem_get_aperture *args = data;
  96. if (!(dev->driver->driver_features & DRIVER_GEM))
  97. return -ENODEV;
  98. args->aper_size = dev->gtt_total;
  99. args->aper_available_size = (args->aper_size -
  100. atomic_read(&dev->pin_memory));
  101. return 0;
  102. }
  103. /**
  104. * Creates a new mm object and returns a handle to it.
  105. */
  106. int
  107. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  108. struct drm_file *file_priv)
  109. {
  110. struct drm_i915_gem_create *args = data;
  111. struct drm_gem_object *obj;
  112. int ret;
  113. u32 handle;
  114. args->size = roundup(args->size, PAGE_SIZE);
  115. /* Allocate the new object */
  116. obj = i915_gem_alloc_object(dev, args->size);
  117. if (obj == NULL)
  118. return -ENOMEM;
  119. ret = drm_gem_handle_create(file_priv, obj, &handle);
  120. if (ret) {
  121. drm_gem_object_unreference_unlocked(obj);
  122. return ret;
  123. }
  124. /* Sink the floating reference from kref_init(handlecount) */
  125. drm_gem_object_handle_unreference_unlocked(obj);
  126. args->handle = handle;
  127. return 0;
  128. }
  129. static inline int
  130. fast_shmem_read(struct page **pages,
  131. loff_t page_base, int page_offset,
  132. char __user *data,
  133. int length)
  134. {
  135. char __iomem *vaddr;
  136. int unwritten;
  137. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  138. if (vaddr == NULL)
  139. return -ENOMEM;
  140. unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
  141. kunmap_atomic(vaddr, KM_USER0);
  142. if (unwritten)
  143. return -EFAULT;
  144. return 0;
  145. }
  146. static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
  147. {
  148. drm_i915_private_t *dev_priv = obj->dev->dev_private;
  149. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  150. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  151. obj_priv->tiling_mode != I915_TILING_NONE;
  152. }
  153. static inline void
  154. slow_shmem_copy(struct page *dst_page,
  155. int dst_offset,
  156. struct page *src_page,
  157. int src_offset,
  158. int length)
  159. {
  160. char *dst_vaddr, *src_vaddr;
  161. dst_vaddr = kmap(dst_page);
  162. src_vaddr = kmap(src_page);
  163. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  164. kunmap(src_page);
  165. kunmap(dst_page);
  166. }
  167. static inline void
  168. slow_shmem_bit17_copy(struct page *gpu_page,
  169. int gpu_offset,
  170. struct page *cpu_page,
  171. int cpu_offset,
  172. int length,
  173. int is_read)
  174. {
  175. char *gpu_vaddr, *cpu_vaddr;
  176. /* Use the unswizzled path if this page isn't affected. */
  177. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  178. if (is_read)
  179. return slow_shmem_copy(cpu_page, cpu_offset,
  180. gpu_page, gpu_offset, length);
  181. else
  182. return slow_shmem_copy(gpu_page, gpu_offset,
  183. cpu_page, cpu_offset, length);
  184. }
  185. gpu_vaddr = kmap(gpu_page);
  186. cpu_vaddr = kmap(cpu_page);
  187. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  188. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  189. */
  190. while (length > 0) {
  191. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  192. int this_length = min(cacheline_end - gpu_offset, length);
  193. int swizzled_gpu_offset = gpu_offset ^ 64;
  194. if (is_read) {
  195. memcpy(cpu_vaddr + cpu_offset,
  196. gpu_vaddr + swizzled_gpu_offset,
  197. this_length);
  198. } else {
  199. memcpy(gpu_vaddr + swizzled_gpu_offset,
  200. cpu_vaddr + cpu_offset,
  201. this_length);
  202. }
  203. cpu_offset += this_length;
  204. gpu_offset += this_length;
  205. length -= this_length;
  206. }
  207. kunmap(cpu_page);
  208. kunmap(gpu_page);
  209. }
  210. /**
  211. * This is the fast shmem pread path, which attempts to copy_from_user directly
  212. * from the backing pages of the object to the user's address space. On a
  213. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  214. */
  215. static int
  216. i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
  217. struct drm_i915_gem_pread *args,
  218. struct drm_file *file_priv)
  219. {
  220. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  221. ssize_t remain;
  222. loff_t offset, page_base;
  223. char __user *user_data;
  224. int page_offset, page_length;
  225. int ret;
  226. user_data = (char __user *) (uintptr_t) args->data_ptr;
  227. remain = args->size;
  228. mutex_lock(&dev->struct_mutex);
  229. ret = i915_gem_object_get_pages(obj, 0);
  230. if (ret != 0)
  231. goto fail_unlock;
  232. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  233. args->size);
  234. if (ret != 0)
  235. goto fail_put_pages;
  236. obj_priv = to_intel_bo(obj);
  237. offset = args->offset;
  238. while (remain > 0) {
  239. /* Operation in this page
  240. *
  241. * page_base = page offset within aperture
  242. * page_offset = offset within page
  243. * page_length = bytes to copy for this page
  244. */
  245. page_base = (offset & ~(PAGE_SIZE-1));
  246. page_offset = offset & (PAGE_SIZE-1);
  247. page_length = remain;
  248. if ((page_offset + remain) > PAGE_SIZE)
  249. page_length = PAGE_SIZE - page_offset;
  250. ret = fast_shmem_read(obj_priv->pages,
  251. page_base, page_offset,
  252. user_data, page_length);
  253. if (ret)
  254. goto fail_put_pages;
  255. remain -= page_length;
  256. user_data += page_length;
  257. offset += page_length;
  258. }
  259. fail_put_pages:
  260. i915_gem_object_put_pages(obj);
  261. fail_unlock:
  262. mutex_unlock(&dev->struct_mutex);
  263. return ret;
  264. }
  265. static int
  266. i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
  267. {
  268. int ret;
  269. ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
  270. /* If we've insufficient memory to map in the pages, attempt
  271. * to make some space by throwing out some old buffers.
  272. */
  273. if (ret == -ENOMEM) {
  274. struct drm_device *dev = obj->dev;
  275. ret = i915_gem_evict_something(dev, obj->size,
  276. i915_gem_get_gtt_alignment(obj));
  277. if (ret)
  278. return ret;
  279. ret = i915_gem_object_get_pages(obj, 0);
  280. }
  281. return ret;
  282. }
  283. /**
  284. * This is the fallback shmem pread path, which allocates temporary storage
  285. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  286. * can copy out of the object's backing pages while holding the struct mutex
  287. * and not take page faults.
  288. */
  289. static int
  290. i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
  291. struct drm_i915_gem_pread *args,
  292. struct drm_file *file_priv)
  293. {
  294. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  295. struct mm_struct *mm = current->mm;
  296. struct page **user_pages;
  297. ssize_t remain;
  298. loff_t offset, pinned_pages, i;
  299. loff_t first_data_page, last_data_page, num_pages;
  300. int shmem_page_index, shmem_page_offset;
  301. int data_page_index, data_page_offset;
  302. int page_length;
  303. int ret;
  304. uint64_t data_ptr = args->data_ptr;
  305. int do_bit17_swizzling;
  306. remain = args->size;
  307. /* Pin the user pages containing the data. We can't fault while
  308. * holding the struct mutex, yet we want to hold it while
  309. * dereferencing the user data.
  310. */
  311. first_data_page = data_ptr / PAGE_SIZE;
  312. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  313. num_pages = last_data_page - first_data_page + 1;
  314. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  315. if (user_pages == NULL)
  316. return -ENOMEM;
  317. down_read(&mm->mmap_sem);
  318. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  319. num_pages, 1, 0, user_pages, NULL);
  320. up_read(&mm->mmap_sem);
  321. if (pinned_pages < num_pages) {
  322. ret = -EFAULT;
  323. goto fail_put_user_pages;
  324. }
  325. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  326. mutex_lock(&dev->struct_mutex);
  327. ret = i915_gem_object_get_pages_or_evict(obj);
  328. if (ret)
  329. goto fail_unlock;
  330. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  331. args->size);
  332. if (ret != 0)
  333. goto fail_put_pages;
  334. obj_priv = to_intel_bo(obj);
  335. offset = args->offset;
  336. while (remain > 0) {
  337. /* Operation in this page
  338. *
  339. * shmem_page_index = page number within shmem file
  340. * shmem_page_offset = offset within page in shmem file
  341. * data_page_index = page number in get_user_pages return
  342. * data_page_offset = offset with data_page_index page.
  343. * page_length = bytes to copy for this page
  344. */
  345. shmem_page_index = offset / PAGE_SIZE;
  346. shmem_page_offset = offset & ~PAGE_MASK;
  347. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  348. data_page_offset = data_ptr & ~PAGE_MASK;
  349. page_length = remain;
  350. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  351. page_length = PAGE_SIZE - shmem_page_offset;
  352. if ((data_page_offset + page_length) > PAGE_SIZE)
  353. page_length = PAGE_SIZE - data_page_offset;
  354. if (do_bit17_swizzling) {
  355. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  356. shmem_page_offset,
  357. user_pages[data_page_index],
  358. data_page_offset,
  359. page_length,
  360. 1);
  361. } else {
  362. slow_shmem_copy(user_pages[data_page_index],
  363. data_page_offset,
  364. obj_priv->pages[shmem_page_index],
  365. shmem_page_offset,
  366. page_length);
  367. }
  368. remain -= page_length;
  369. data_ptr += page_length;
  370. offset += page_length;
  371. }
  372. fail_put_pages:
  373. i915_gem_object_put_pages(obj);
  374. fail_unlock:
  375. mutex_unlock(&dev->struct_mutex);
  376. fail_put_user_pages:
  377. for (i = 0; i < pinned_pages; i++) {
  378. SetPageDirty(user_pages[i]);
  379. page_cache_release(user_pages[i]);
  380. }
  381. drm_free_large(user_pages);
  382. return ret;
  383. }
  384. /**
  385. * Reads data from the object referenced by handle.
  386. *
  387. * On error, the contents of *data are undefined.
  388. */
  389. int
  390. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  391. struct drm_file *file_priv)
  392. {
  393. struct drm_i915_gem_pread *args = data;
  394. struct drm_gem_object *obj;
  395. struct drm_i915_gem_object *obj_priv;
  396. int ret;
  397. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  398. if (obj == NULL)
  399. return -ENOENT;
  400. obj_priv = to_intel_bo(obj);
  401. /* Bounds check source.
  402. *
  403. * XXX: This could use review for overflow issues...
  404. */
  405. if (args->offset > obj->size || args->size > obj->size ||
  406. args->offset + args->size > obj->size) {
  407. drm_gem_object_unreference_unlocked(obj);
  408. return -EINVAL;
  409. }
  410. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  411. ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
  412. } else {
  413. ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
  414. if (ret != 0)
  415. ret = i915_gem_shmem_pread_slow(dev, obj, args,
  416. file_priv);
  417. }
  418. drm_gem_object_unreference_unlocked(obj);
  419. return ret;
  420. }
  421. /* This is the fast write path which cannot handle
  422. * page faults in the source data
  423. */
  424. static inline int
  425. fast_user_write(struct io_mapping *mapping,
  426. loff_t page_base, int page_offset,
  427. char __user *user_data,
  428. int length)
  429. {
  430. char *vaddr_atomic;
  431. unsigned long unwritten;
  432. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
  433. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  434. user_data, length);
  435. io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
  436. if (unwritten)
  437. return -EFAULT;
  438. return 0;
  439. }
  440. /* Here's the write path which can sleep for
  441. * page faults
  442. */
  443. static inline void
  444. slow_kernel_write(struct io_mapping *mapping,
  445. loff_t gtt_base, int gtt_offset,
  446. struct page *user_page, int user_offset,
  447. int length)
  448. {
  449. char __iomem *dst_vaddr;
  450. char *src_vaddr;
  451. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  452. src_vaddr = kmap(user_page);
  453. memcpy_toio(dst_vaddr + gtt_offset,
  454. src_vaddr + user_offset,
  455. length);
  456. kunmap(user_page);
  457. io_mapping_unmap(dst_vaddr);
  458. }
  459. static inline int
  460. fast_shmem_write(struct page **pages,
  461. loff_t page_base, int page_offset,
  462. char __user *data,
  463. int length)
  464. {
  465. char __iomem *vaddr;
  466. unsigned long unwritten;
  467. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  468. if (vaddr == NULL)
  469. return -ENOMEM;
  470. unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
  471. kunmap_atomic(vaddr, KM_USER0);
  472. if (unwritten)
  473. return -EFAULT;
  474. return 0;
  475. }
  476. /**
  477. * This is the fast pwrite path, where we copy the data directly from the
  478. * user into the GTT, uncached.
  479. */
  480. static int
  481. i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  482. struct drm_i915_gem_pwrite *args,
  483. struct drm_file *file_priv)
  484. {
  485. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  486. drm_i915_private_t *dev_priv = dev->dev_private;
  487. ssize_t remain;
  488. loff_t offset, page_base;
  489. char __user *user_data;
  490. int page_offset, page_length;
  491. int ret;
  492. user_data = (char __user *) (uintptr_t) args->data_ptr;
  493. remain = args->size;
  494. if (!access_ok(VERIFY_READ, user_data, remain))
  495. return -EFAULT;
  496. mutex_lock(&dev->struct_mutex);
  497. ret = i915_gem_object_pin(obj, 0);
  498. if (ret) {
  499. mutex_unlock(&dev->struct_mutex);
  500. return ret;
  501. }
  502. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  503. if (ret)
  504. goto fail;
  505. obj_priv = to_intel_bo(obj);
  506. offset = obj_priv->gtt_offset + args->offset;
  507. while (remain > 0) {
  508. /* Operation in this page
  509. *
  510. * page_base = page offset within aperture
  511. * page_offset = offset within page
  512. * page_length = bytes to copy for this page
  513. */
  514. page_base = (offset & ~(PAGE_SIZE-1));
  515. page_offset = offset & (PAGE_SIZE-1);
  516. page_length = remain;
  517. if ((page_offset + remain) > PAGE_SIZE)
  518. page_length = PAGE_SIZE - page_offset;
  519. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  520. page_offset, user_data, page_length);
  521. /* If we get a fault while copying data, then (presumably) our
  522. * source page isn't available. Return the error and we'll
  523. * retry in the slow path.
  524. */
  525. if (ret)
  526. goto fail;
  527. remain -= page_length;
  528. user_data += page_length;
  529. offset += page_length;
  530. }
  531. fail:
  532. i915_gem_object_unpin(obj);
  533. mutex_unlock(&dev->struct_mutex);
  534. return ret;
  535. }
  536. /**
  537. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  538. * the memory and maps it using kmap_atomic for copying.
  539. *
  540. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  541. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  542. */
  543. static int
  544. i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  545. struct drm_i915_gem_pwrite *args,
  546. struct drm_file *file_priv)
  547. {
  548. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  549. drm_i915_private_t *dev_priv = dev->dev_private;
  550. ssize_t remain;
  551. loff_t gtt_page_base, offset;
  552. loff_t first_data_page, last_data_page, num_pages;
  553. loff_t pinned_pages, i;
  554. struct page **user_pages;
  555. struct mm_struct *mm = current->mm;
  556. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  557. int ret;
  558. uint64_t data_ptr = args->data_ptr;
  559. remain = args->size;
  560. /* Pin the user pages containing the data. We can't fault while
  561. * holding the struct mutex, and all of the pwrite implementations
  562. * want to hold it while dereferencing the user data.
  563. */
  564. first_data_page = data_ptr / PAGE_SIZE;
  565. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  566. num_pages = last_data_page - first_data_page + 1;
  567. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  568. if (user_pages == NULL)
  569. return -ENOMEM;
  570. down_read(&mm->mmap_sem);
  571. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  572. num_pages, 0, 0, user_pages, NULL);
  573. up_read(&mm->mmap_sem);
  574. if (pinned_pages < num_pages) {
  575. ret = -EFAULT;
  576. goto out_unpin_pages;
  577. }
  578. mutex_lock(&dev->struct_mutex);
  579. ret = i915_gem_object_pin(obj, 0);
  580. if (ret)
  581. goto out_unlock;
  582. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  583. if (ret)
  584. goto out_unpin_object;
  585. obj_priv = to_intel_bo(obj);
  586. offset = obj_priv->gtt_offset + args->offset;
  587. while (remain > 0) {
  588. /* Operation in this page
  589. *
  590. * gtt_page_base = page offset within aperture
  591. * gtt_page_offset = offset within page in aperture
  592. * data_page_index = page number in get_user_pages return
  593. * data_page_offset = offset with data_page_index page.
  594. * page_length = bytes to copy for this page
  595. */
  596. gtt_page_base = offset & PAGE_MASK;
  597. gtt_page_offset = offset & ~PAGE_MASK;
  598. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  599. data_page_offset = data_ptr & ~PAGE_MASK;
  600. page_length = remain;
  601. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  602. page_length = PAGE_SIZE - gtt_page_offset;
  603. if ((data_page_offset + page_length) > PAGE_SIZE)
  604. page_length = PAGE_SIZE - data_page_offset;
  605. slow_kernel_write(dev_priv->mm.gtt_mapping,
  606. gtt_page_base, gtt_page_offset,
  607. user_pages[data_page_index],
  608. data_page_offset,
  609. page_length);
  610. remain -= page_length;
  611. offset += page_length;
  612. data_ptr += page_length;
  613. }
  614. out_unpin_object:
  615. i915_gem_object_unpin(obj);
  616. out_unlock:
  617. mutex_unlock(&dev->struct_mutex);
  618. out_unpin_pages:
  619. for (i = 0; i < pinned_pages; i++)
  620. page_cache_release(user_pages[i]);
  621. drm_free_large(user_pages);
  622. return ret;
  623. }
  624. /**
  625. * This is the fast shmem pwrite path, which attempts to directly
  626. * copy_from_user into the kmapped pages backing the object.
  627. */
  628. static int
  629. i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  630. struct drm_i915_gem_pwrite *args,
  631. struct drm_file *file_priv)
  632. {
  633. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  634. ssize_t remain;
  635. loff_t offset, page_base;
  636. char __user *user_data;
  637. int page_offset, page_length;
  638. int ret;
  639. user_data = (char __user *) (uintptr_t) args->data_ptr;
  640. remain = args->size;
  641. mutex_lock(&dev->struct_mutex);
  642. ret = i915_gem_object_get_pages(obj, 0);
  643. if (ret != 0)
  644. goto fail_unlock;
  645. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  646. if (ret != 0)
  647. goto fail_put_pages;
  648. obj_priv = to_intel_bo(obj);
  649. offset = args->offset;
  650. obj_priv->dirty = 1;
  651. while (remain > 0) {
  652. /* Operation in this page
  653. *
  654. * page_base = page offset within aperture
  655. * page_offset = offset within page
  656. * page_length = bytes to copy for this page
  657. */
  658. page_base = (offset & ~(PAGE_SIZE-1));
  659. page_offset = offset & (PAGE_SIZE-1);
  660. page_length = remain;
  661. if ((page_offset + remain) > PAGE_SIZE)
  662. page_length = PAGE_SIZE - page_offset;
  663. ret = fast_shmem_write(obj_priv->pages,
  664. page_base, page_offset,
  665. user_data, page_length);
  666. if (ret)
  667. goto fail_put_pages;
  668. remain -= page_length;
  669. user_data += page_length;
  670. offset += page_length;
  671. }
  672. fail_put_pages:
  673. i915_gem_object_put_pages(obj);
  674. fail_unlock:
  675. mutex_unlock(&dev->struct_mutex);
  676. return ret;
  677. }
  678. /**
  679. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  680. * the memory and maps it using kmap_atomic for copying.
  681. *
  682. * This avoids taking mmap_sem for faulting on the user's address while the
  683. * struct_mutex is held.
  684. */
  685. static int
  686. i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  687. struct drm_i915_gem_pwrite *args,
  688. struct drm_file *file_priv)
  689. {
  690. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  691. struct mm_struct *mm = current->mm;
  692. struct page **user_pages;
  693. ssize_t remain;
  694. loff_t offset, pinned_pages, i;
  695. loff_t first_data_page, last_data_page, num_pages;
  696. int shmem_page_index, shmem_page_offset;
  697. int data_page_index, data_page_offset;
  698. int page_length;
  699. int ret;
  700. uint64_t data_ptr = args->data_ptr;
  701. int do_bit17_swizzling;
  702. remain = args->size;
  703. /* Pin the user pages containing the data. We can't fault while
  704. * holding the struct mutex, and all of the pwrite implementations
  705. * want to hold it while dereferencing the user data.
  706. */
  707. first_data_page = data_ptr / PAGE_SIZE;
  708. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  709. num_pages = last_data_page - first_data_page + 1;
  710. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  711. if (user_pages == NULL)
  712. return -ENOMEM;
  713. down_read(&mm->mmap_sem);
  714. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  715. num_pages, 0, 0, user_pages, NULL);
  716. up_read(&mm->mmap_sem);
  717. if (pinned_pages < num_pages) {
  718. ret = -EFAULT;
  719. goto fail_put_user_pages;
  720. }
  721. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  722. mutex_lock(&dev->struct_mutex);
  723. ret = i915_gem_object_get_pages_or_evict(obj);
  724. if (ret)
  725. goto fail_unlock;
  726. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  727. if (ret != 0)
  728. goto fail_put_pages;
  729. obj_priv = to_intel_bo(obj);
  730. offset = args->offset;
  731. obj_priv->dirty = 1;
  732. while (remain > 0) {
  733. /* Operation in this page
  734. *
  735. * shmem_page_index = page number within shmem file
  736. * shmem_page_offset = offset within page in shmem file
  737. * data_page_index = page number in get_user_pages return
  738. * data_page_offset = offset with data_page_index page.
  739. * page_length = bytes to copy for this page
  740. */
  741. shmem_page_index = offset / PAGE_SIZE;
  742. shmem_page_offset = offset & ~PAGE_MASK;
  743. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  744. data_page_offset = data_ptr & ~PAGE_MASK;
  745. page_length = remain;
  746. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  747. page_length = PAGE_SIZE - shmem_page_offset;
  748. if ((data_page_offset + page_length) > PAGE_SIZE)
  749. page_length = PAGE_SIZE - data_page_offset;
  750. if (do_bit17_swizzling) {
  751. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  752. shmem_page_offset,
  753. user_pages[data_page_index],
  754. data_page_offset,
  755. page_length,
  756. 0);
  757. } else {
  758. slow_shmem_copy(obj_priv->pages[shmem_page_index],
  759. shmem_page_offset,
  760. user_pages[data_page_index],
  761. data_page_offset,
  762. page_length);
  763. }
  764. remain -= page_length;
  765. data_ptr += page_length;
  766. offset += page_length;
  767. }
  768. fail_put_pages:
  769. i915_gem_object_put_pages(obj);
  770. fail_unlock:
  771. mutex_unlock(&dev->struct_mutex);
  772. fail_put_user_pages:
  773. for (i = 0; i < pinned_pages; i++)
  774. page_cache_release(user_pages[i]);
  775. drm_free_large(user_pages);
  776. return ret;
  777. }
  778. /**
  779. * Writes data to the object referenced by handle.
  780. *
  781. * On error, the contents of the buffer that were to be modified are undefined.
  782. */
  783. int
  784. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  785. struct drm_file *file_priv)
  786. {
  787. struct drm_i915_gem_pwrite *args = data;
  788. struct drm_gem_object *obj;
  789. struct drm_i915_gem_object *obj_priv;
  790. int ret = 0;
  791. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  792. if (obj == NULL)
  793. return -ENOENT;
  794. obj_priv = to_intel_bo(obj);
  795. /* Bounds check destination.
  796. *
  797. * XXX: This could use review for overflow issues...
  798. */
  799. if (args->offset > obj->size || args->size > obj->size ||
  800. args->offset + args->size > obj->size) {
  801. drm_gem_object_unreference_unlocked(obj);
  802. return -EINVAL;
  803. }
  804. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  805. * it would end up going through the fenced access, and we'll get
  806. * different detiling behavior between reading and writing.
  807. * pread/pwrite currently are reading and writing from the CPU
  808. * perspective, requiring manual detiling by the client.
  809. */
  810. if (obj_priv->phys_obj)
  811. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  812. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  813. dev->gtt_total != 0 &&
  814. obj->write_domain != I915_GEM_DOMAIN_CPU) {
  815. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
  816. if (ret == -EFAULT) {
  817. ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
  818. file_priv);
  819. }
  820. } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
  821. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
  822. } else {
  823. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
  824. if (ret == -EFAULT) {
  825. ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
  826. file_priv);
  827. }
  828. }
  829. #if WATCH_PWRITE
  830. if (ret)
  831. DRM_INFO("pwrite failed %d\n", ret);
  832. #endif
  833. drm_gem_object_unreference_unlocked(obj);
  834. return ret;
  835. }
  836. /**
  837. * Called when user space prepares to use an object with the CPU, either
  838. * through the mmap ioctl's mapping or a GTT mapping.
  839. */
  840. int
  841. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  842. struct drm_file *file_priv)
  843. {
  844. struct drm_i915_private *dev_priv = dev->dev_private;
  845. struct drm_i915_gem_set_domain *args = data;
  846. struct drm_gem_object *obj;
  847. struct drm_i915_gem_object *obj_priv;
  848. uint32_t read_domains = args->read_domains;
  849. uint32_t write_domain = args->write_domain;
  850. int ret;
  851. if (!(dev->driver->driver_features & DRIVER_GEM))
  852. return -ENODEV;
  853. /* Only handle setting domains to types used by the CPU. */
  854. if (write_domain & I915_GEM_GPU_DOMAINS)
  855. return -EINVAL;
  856. if (read_domains & I915_GEM_GPU_DOMAINS)
  857. return -EINVAL;
  858. /* Having something in the write domain implies it's in the read
  859. * domain, and only that read domain. Enforce that in the request.
  860. */
  861. if (write_domain != 0 && read_domains != write_domain)
  862. return -EINVAL;
  863. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  864. if (obj == NULL)
  865. return -ENOENT;
  866. obj_priv = to_intel_bo(obj);
  867. mutex_lock(&dev->struct_mutex);
  868. intel_mark_busy(dev, obj);
  869. #if WATCH_BUF
  870. DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
  871. obj, obj->size, read_domains, write_domain);
  872. #endif
  873. if (read_domains & I915_GEM_DOMAIN_GTT) {
  874. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  875. /* Update the LRU on the fence for the CPU access that's
  876. * about to occur.
  877. */
  878. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  879. struct drm_i915_fence_reg *reg =
  880. &dev_priv->fence_regs[obj_priv->fence_reg];
  881. list_move_tail(&reg->lru_list,
  882. &dev_priv->mm.fence_list);
  883. }
  884. /* Silently promote "you're not bound, there was nothing to do"
  885. * to success, since the client was just asking us to
  886. * make sure everything was done.
  887. */
  888. if (ret == -EINVAL)
  889. ret = 0;
  890. } else {
  891. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  892. }
  893. /* Maintain LRU order of "inactive" objects */
  894. if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
  895. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  896. drm_gem_object_unreference(obj);
  897. mutex_unlock(&dev->struct_mutex);
  898. return ret;
  899. }
  900. /**
  901. * Called when user space has done writes to this buffer
  902. */
  903. int
  904. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  905. struct drm_file *file_priv)
  906. {
  907. struct drm_i915_gem_sw_finish *args = data;
  908. struct drm_gem_object *obj;
  909. struct drm_i915_gem_object *obj_priv;
  910. int ret = 0;
  911. if (!(dev->driver->driver_features & DRIVER_GEM))
  912. return -ENODEV;
  913. mutex_lock(&dev->struct_mutex);
  914. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  915. if (obj == NULL) {
  916. mutex_unlock(&dev->struct_mutex);
  917. return -ENOENT;
  918. }
  919. #if WATCH_BUF
  920. DRM_INFO("%s: sw_finish %d (%p %zd)\n",
  921. __func__, args->handle, obj, obj->size);
  922. #endif
  923. obj_priv = to_intel_bo(obj);
  924. /* Pinned buffers may be scanout, so flush the cache */
  925. if (obj_priv->pin_count)
  926. i915_gem_object_flush_cpu_write_domain(obj);
  927. drm_gem_object_unreference(obj);
  928. mutex_unlock(&dev->struct_mutex);
  929. return ret;
  930. }
  931. /**
  932. * Maps the contents of an object, returning the address it is mapped
  933. * into.
  934. *
  935. * While the mapping holds a reference on the contents of the object, it doesn't
  936. * imply a ref on the object itself.
  937. */
  938. int
  939. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  940. struct drm_file *file_priv)
  941. {
  942. struct drm_i915_gem_mmap *args = data;
  943. struct drm_gem_object *obj;
  944. loff_t offset;
  945. unsigned long addr;
  946. if (!(dev->driver->driver_features & DRIVER_GEM))
  947. return -ENODEV;
  948. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  949. if (obj == NULL)
  950. return -ENOENT;
  951. offset = args->offset;
  952. down_write(&current->mm->mmap_sem);
  953. addr = do_mmap(obj->filp, 0, args->size,
  954. PROT_READ | PROT_WRITE, MAP_SHARED,
  955. args->offset);
  956. up_write(&current->mm->mmap_sem);
  957. drm_gem_object_unreference_unlocked(obj);
  958. if (IS_ERR((void *)addr))
  959. return addr;
  960. args->addr_ptr = (uint64_t) addr;
  961. return 0;
  962. }
  963. /**
  964. * i915_gem_fault - fault a page into the GTT
  965. * vma: VMA in question
  966. * vmf: fault info
  967. *
  968. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  969. * from userspace. The fault handler takes care of binding the object to
  970. * the GTT (if needed), allocating and programming a fence register (again,
  971. * only if needed based on whether the old reg is still valid or the object
  972. * is tiled) and inserting a new PTE into the faulting process.
  973. *
  974. * Note that the faulting process may involve evicting existing objects
  975. * from the GTT and/or fence registers to make room. So performance may
  976. * suffer if the GTT working set is large or there are few fence registers
  977. * left.
  978. */
  979. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  980. {
  981. struct drm_gem_object *obj = vma->vm_private_data;
  982. struct drm_device *dev = obj->dev;
  983. drm_i915_private_t *dev_priv = dev->dev_private;
  984. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  985. pgoff_t page_offset;
  986. unsigned long pfn;
  987. int ret = 0;
  988. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  989. /* We don't use vmf->pgoff since that has the fake offset */
  990. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  991. PAGE_SHIFT;
  992. /* Now bind it into the GTT if needed */
  993. mutex_lock(&dev->struct_mutex);
  994. if (!obj_priv->gtt_space) {
  995. ret = i915_gem_object_bind_to_gtt(obj, 0);
  996. if (ret)
  997. goto unlock;
  998. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  999. if (ret)
  1000. goto unlock;
  1001. }
  1002. /* Need a new fence register? */
  1003. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1004. ret = i915_gem_object_get_fence_reg(obj, true);
  1005. if (ret)
  1006. goto unlock;
  1007. }
  1008. if (i915_gem_object_is_inactive(obj_priv))
  1009. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1010. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  1011. page_offset;
  1012. /* Finally, remap it using the new GTT offset */
  1013. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1014. unlock:
  1015. mutex_unlock(&dev->struct_mutex);
  1016. switch (ret) {
  1017. case 0:
  1018. case -ERESTARTSYS:
  1019. return VM_FAULT_NOPAGE;
  1020. case -ENOMEM:
  1021. case -EAGAIN:
  1022. return VM_FAULT_OOM;
  1023. default:
  1024. return VM_FAULT_SIGBUS;
  1025. }
  1026. }
  1027. /**
  1028. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1029. * @obj: obj in question
  1030. *
  1031. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1032. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1033. * up the object based on the offset and sets up the various memory mapping
  1034. * structures.
  1035. *
  1036. * This routine allocates and attaches a fake offset for @obj.
  1037. */
  1038. static int
  1039. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  1040. {
  1041. struct drm_device *dev = obj->dev;
  1042. struct drm_gem_mm *mm = dev->mm_private;
  1043. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1044. struct drm_map_list *list;
  1045. struct drm_local_map *map;
  1046. int ret = 0;
  1047. /* Set the object up for mmap'ing */
  1048. list = &obj->map_list;
  1049. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1050. if (!list->map)
  1051. return -ENOMEM;
  1052. map = list->map;
  1053. map->type = _DRM_GEM;
  1054. map->size = obj->size;
  1055. map->handle = obj;
  1056. /* Get a DRM GEM mmap offset allocated... */
  1057. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1058. obj->size / PAGE_SIZE, 0, 0);
  1059. if (!list->file_offset_node) {
  1060. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  1061. ret = -ENOMEM;
  1062. goto out_free_list;
  1063. }
  1064. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1065. obj->size / PAGE_SIZE, 0);
  1066. if (!list->file_offset_node) {
  1067. ret = -ENOMEM;
  1068. goto out_free_list;
  1069. }
  1070. list->hash.key = list->file_offset_node->start;
  1071. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  1072. DRM_ERROR("failed to add to map hash\n");
  1073. ret = -ENOMEM;
  1074. goto out_free_mm;
  1075. }
  1076. /* By now we should be all set, any drm_mmap request on the offset
  1077. * below will get to our mmap & fault handler */
  1078. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  1079. return 0;
  1080. out_free_mm:
  1081. drm_mm_put_block(list->file_offset_node);
  1082. out_free_list:
  1083. kfree(list->map);
  1084. return ret;
  1085. }
  1086. /**
  1087. * i915_gem_release_mmap - remove physical page mappings
  1088. * @obj: obj in question
  1089. *
  1090. * Preserve the reservation of the mmapping with the DRM core code, but
  1091. * relinquish ownership of the pages back to the system.
  1092. *
  1093. * It is vital that we remove the page mapping if we have mapped a tiled
  1094. * object through the GTT and then lose the fence register due to
  1095. * resource pressure. Similarly if the object has been moved out of the
  1096. * aperture, than pages mapped into userspace must be revoked. Removing the
  1097. * mapping will then trigger a page fault on the next user access, allowing
  1098. * fixup by i915_gem_fault().
  1099. */
  1100. void
  1101. i915_gem_release_mmap(struct drm_gem_object *obj)
  1102. {
  1103. struct drm_device *dev = obj->dev;
  1104. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1105. if (dev->dev_mapping)
  1106. unmap_mapping_range(dev->dev_mapping,
  1107. obj_priv->mmap_offset, obj->size, 1);
  1108. }
  1109. static void
  1110. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  1111. {
  1112. struct drm_device *dev = obj->dev;
  1113. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1114. struct drm_gem_mm *mm = dev->mm_private;
  1115. struct drm_map_list *list;
  1116. list = &obj->map_list;
  1117. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1118. if (list->file_offset_node) {
  1119. drm_mm_put_block(list->file_offset_node);
  1120. list->file_offset_node = NULL;
  1121. }
  1122. if (list->map) {
  1123. kfree(list->map);
  1124. list->map = NULL;
  1125. }
  1126. obj_priv->mmap_offset = 0;
  1127. }
  1128. /**
  1129. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1130. * @obj: object to check
  1131. *
  1132. * Return the required GTT alignment for an object, taking into account
  1133. * potential fence register mapping if needed.
  1134. */
  1135. static uint32_t
  1136. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  1137. {
  1138. struct drm_device *dev = obj->dev;
  1139. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1140. int start, i;
  1141. /*
  1142. * Minimum alignment is 4k (GTT page size), but might be greater
  1143. * if a fence register is needed for the object.
  1144. */
  1145. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  1146. return 4096;
  1147. /*
  1148. * Previous chips need to be aligned to the size of the smallest
  1149. * fence register that can contain the object.
  1150. */
  1151. if (IS_I9XX(dev))
  1152. start = 1024*1024;
  1153. else
  1154. start = 512*1024;
  1155. for (i = start; i < obj->size; i <<= 1)
  1156. ;
  1157. return i;
  1158. }
  1159. /**
  1160. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1161. * @dev: DRM device
  1162. * @data: GTT mapping ioctl data
  1163. * @file_priv: GEM object info
  1164. *
  1165. * Simply returns the fake offset to userspace so it can mmap it.
  1166. * The mmap call will end up in drm_gem_mmap(), which will set things
  1167. * up so we can get faults in the handler above.
  1168. *
  1169. * The fault handler will take care of binding the object into the GTT
  1170. * (since it may have been evicted to make room for something), allocating
  1171. * a fence register, and mapping the appropriate aperture address into
  1172. * userspace.
  1173. */
  1174. int
  1175. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1176. struct drm_file *file_priv)
  1177. {
  1178. struct drm_i915_gem_mmap_gtt *args = data;
  1179. struct drm_gem_object *obj;
  1180. struct drm_i915_gem_object *obj_priv;
  1181. int ret;
  1182. if (!(dev->driver->driver_features & DRIVER_GEM))
  1183. return -ENODEV;
  1184. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1185. if (obj == NULL)
  1186. return -ENOENT;
  1187. mutex_lock(&dev->struct_mutex);
  1188. obj_priv = to_intel_bo(obj);
  1189. if (obj_priv->madv != I915_MADV_WILLNEED) {
  1190. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1191. drm_gem_object_unreference(obj);
  1192. mutex_unlock(&dev->struct_mutex);
  1193. return -EINVAL;
  1194. }
  1195. if (!obj_priv->mmap_offset) {
  1196. ret = i915_gem_create_mmap_offset(obj);
  1197. if (ret) {
  1198. drm_gem_object_unreference(obj);
  1199. mutex_unlock(&dev->struct_mutex);
  1200. return ret;
  1201. }
  1202. }
  1203. args->offset = obj_priv->mmap_offset;
  1204. /*
  1205. * Pull it into the GTT so that we have a page list (makes the
  1206. * initial fault faster and any subsequent flushing possible).
  1207. */
  1208. if (!obj_priv->agp_mem) {
  1209. ret = i915_gem_object_bind_to_gtt(obj, 0);
  1210. if (ret) {
  1211. drm_gem_object_unreference(obj);
  1212. mutex_unlock(&dev->struct_mutex);
  1213. return ret;
  1214. }
  1215. }
  1216. drm_gem_object_unreference(obj);
  1217. mutex_unlock(&dev->struct_mutex);
  1218. return 0;
  1219. }
  1220. void
  1221. i915_gem_object_put_pages(struct drm_gem_object *obj)
  1222. {
  1223. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1224. int page_count = obj->size / PAGE_SIZE;
  1225. int i;
  1226. BUG_ON(obj_priv->pages_refcount == 0);
  1227. BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
  1228. if (--obj_priv->pages_refcount != 0)
  1229. return;
  1230. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1231. i915_gem_object_save_bit_17_swizzle(obj);
  1232. if (obj_priv->madv == I915_MADV_DONTNEED)
  1233. obj_priv->dirty = 0;
  1234. for (i = 0; i < page_count; i++) {
  1235. if (obj_priv->dirty)
  1236. set_page_dirty(obj_priv->pages[i]);
  1237. if (obj_priv->madv == I915_MADV_WILLNEED)
  1238. mark_page_accessed(obj_priv->pages[i]);
  1239. page_cache_release(obj_priv->pages[i]);
  1240. }
  1241. obj_priv->dirty = 0;
  1242. drm_free_large(obj_priv->pages);
  1243. obj_priv->pages = NULL;
  1244. }
  1245. static uint32_t
  1246. i915_gem_next_request_seqno(struct drm_device *dev,
  1247. struct intel_ring_buffer *ring)
  1248. {
  1249. drm_i915_private_t *dev_priv = dev->dev_private;
  1250. ring->outstanding_lazy_request = true;
  1251. return dev_priv->next_seqno;
  1252. }
  1253. static void
  1254. i915_gem_object_move_to_active(struct drm_gem_object *obj,
  1255. struct intel_ring_buffer *ring)
  1256. {
  1257. struct drm_device *dev = obj->dev;
  1258. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1259. uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
  1260. BUG_ON(ring == NULL);
  1261. obj_priv->ring = ring;
  1262. /* Add a reference if we're newly entering the active list. */
  1263. if (!obj_priv->active) {
  1264. drm_gem_object_reference(obj);
  1265. obj_priv->active = 1;
  1266. }
  1267. /* Move from whatever list we were on to the tail of execution. */
  1268. list_move_tail(&obj_priv->list, &ring->active_list);
  1269. obj_priv->last_rendering_seqno = seqno;
  1270. }
  1271. static void
  1272. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  1273. {
  1274. struct drm_device *dev = obj->dev;
  1275. drm_i915_private_t *dev_priv = dev->dev_private;
  1276. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1277. BUG_ON(!obj_priv->active);
  1278. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  1279. obj_priv->last_rendering_seqno = 0;
  1280. }
  1281. /* Immediately discard the backing storage */
  1282. static void
  1283. i915_gem_object_truncate(struct drm_gem_object *obj)
  1284. {
  1285. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1286. struct inode *inode;
  1287. /* Our goal here is to return as much of the memory as
  1288. * is possible back to the system as we are called from OOM.
  1289. * To do this we must instruct the shmfs to drop all of its
  1290. * backing pages, *now*. Here we mirror the actions taken
  1291. * when by shmem_delete_inode() to release the backing store.
  1292. */
  1293. inode = obj->filp->f_path.dentry->d_inode;
  1294. truncate_inode_pages(inode->i_mapping, 0);
  1295. if (inode->i_op->truncate_range)
  1296. inode->i_op->truncate_range(inode, 0, (loff_t)-1);
  1297. obj_priv->madv = __I915_MADV_PURGED;
  1298. }
  1299. static inline int
  1300. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
  1301. {
  1302. return obj_priv->madv == I915_MADV_DONTNEED;
  1303. }
  1304. static void
  1305. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  1306. {
  1307. struct drm_device *dev = obj->dev;
  1308. drm_i915_private_t *dev_priv = dev->dev_private;
  1309. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1310. i915_verify_inactive(dev, __FILE__, __LINE__);
  1311. if (obj_priv->pin_count != 0)
  1312. list_del_init(&obj_priv->list);
  1313. else
  1314. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1315. BUG_ON(!list_empty(&obj_priv->gpu_write_list));
  1316. obj_priv->last_rendering_seqno = 0;
  1317. obj_priv->ring = NULL;
  1318. if (obj_priv->active) {
  1319. obj_priv->active = 0;
  1320. drm_gem_object_unreference(obj);
  1321. }
  1322. i915_verify_inactive(dev, __FILE__, __LINE__);
  1323. }
  1324. void
  1325. i915_gem_process_flushing_list(struct drm_device *dev,
  1326. uint32_t flush_domains,
  1327. struct intel_ring_buffer *ring)
  1328. {
  1329. drm_i915_private_t *dev_priv = dev->dev_private;
  1330. struct drm_i915_gem_object *obj_priv, *next;
  1331. list_for_each_entry_safe(obj_priv, next,
  1332. &dev_priv->mm.gpu_write_list,
  1333. gpu_write_list) {
  1334. struct drm_gem_object *obj = &obj_priv->base;
  1335. if ((obj->write_domain & flush_domains) ==
  1336. obj->write_domain &&
  1337. obj_priv->ring->ring_flag == ring->ring_flag) {
  1338. uint32_t old_write_domain = obj->write_domain;
  1339. obj->write_domain = 0;
  1340. list_del_init(&obj_priv->gpu_write_list);
  1341. i915_gem_object_move_to_active(obj, ring);
  1342. /* update the fence lru list */
  1343. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1344. struct drm_i915_fence_reg *reg =
  1345. &dev_priv->fence_regs[obj_priv->fence_reg];
  1346. list_move_tail(&reg->lru_list,
  1347. &dev_priv->mm.fence_list);
  1348. }
  1349. trace_i915_gem_object_change_domain(obj,
  1350. obj->read_domains,
  1351. old_write_domain);
  1352. }
  1353. }
  1354. }
  1355. uint32_t
  1356. i915_add_request(struct drm_device *dev,
  1357. struct drm_file *file_priv,
  1358. struct drm_i915_gem_request *request,
  1359. struct intel_ring_buffer *ring)
  1360. {
  1361. drm_i915_private_t *dev_priv = dev->dev_private;
  1362. struct drm_i915_file_private *i915_file_priv = NULL;
  1363. uint32_t seqno;
  1364. int was_empty;
  1365. if (file_priv != NULL)
  1366. i915_file_priv = file_priv->driver_priv;
  1367. if (request == NULL) {
  1368. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1369. if (request == NULL)
  1370. return 0;
  1371. }
  1372. seqno = ring->add_request(dev, ring, file_priv, 0);
  1373. request->seqno = seqno;
  1374. request->ring = ring;
  1375. request->emitted_jiffies = jiffies;
  1376. was_empty = list_empty(&ring->request_list);
  1377. list_add_tail(&request->list, &ring->request_list);
  1378. if (i915_file_priv) {
  1379. list_add_tail(&request->client_list,
  1380. &i915_file_priv->mm.request_list);
  1381. } else {
  1382. INIT_LIST_HEAD(&request->client_list);
  1383. }
  1384. if (!dev_priv->mm.suspended) {
  1385. mod_timer(&dev_priv->hangcheck_timer,
  1386. jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1387. if (was_empty)
  1388. queue_delayed_work(dev_priv->wq,
  1389. &dev_priv->mm.retire_work, HZ);
  1390. }
  1391. return seqno;
  1392. }
  1393. /**
  1394. * Command execution barrier
  1395. *
  1396. * Ensures that all commands in the ring are finished
  1397. * before signalling the CPU
  1398. */
  1399. static void
  1400. i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
  1401. {
  1402. uint32_t flush_domains = 0;
  1403. /* The sampler always gets flushed on i965 (sigh) */
  1404. if (IS_I965G(dev))
  1405. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  1406. ring->flush(dev, ring,
  1407. I915_GEM_DOMAIN_COMMAND, flush_domains);
  1408. }
  1409. /**
  1410. * Moves buffers associated only with the given active seqno from the active
  1411. * to inactive list, potentially freeing them.
  1412. */
  1413. static void
  1414. i915_gem_retire_request(struct drm_device *dev,
  1415. struct drm_i915_gem_request *request)
  1416. {
  1417. trace_i915_gem_request_retire(dev, request->seqno);
  1418. /* Move any buffers on the active list that are no longer referenced
  1419. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1420. */
  1421. while (!list_empty(&request->ring->active_list)) {
  1422. struct drm_gem_object *obj;
  1423. struct drm_i915_gem_object *obj_priv;
  1424. obj_priv = list_first_entry(&request->ring->active_list,
  1425. struct drm_i915_gem_object,
  1426. list);
  1427. obj = &obj_priv->base;
  1428. /* If the seqno being retired doesn't match the oldest in the
  1429. * list, then the oldest in the list must still be newer than
  1430. * this seqno.
  1431. */
  1432. if (obj_priv->last_rendering_seqno != request->seqno)
  1433. return;
  1434. #if WATCH_LRU
  1435. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  1436. __func__, request->seqno, obj);
  1437. #endif
  1438. if (obj->write_domain != 0)
  1439. i915_gem_object_move_to_flushing(obj);
  1440. else
  1441. i915_gem_object_move_to_inactive(obj);
  1442. }
  1443. }
  1444. /**
  1445. * Returns true if seq1 is later than seq2.
  1446. */
  1447. bool
  1448. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  1449. {
  1450. return (int32_t)(seq1 - seq2) >= 0;
  1451. }
  1452. uint32_t
  1453. i915_get_gem_seqno(struct drm_device *dev,
  1454. struct intel_ring_buffer *ring)
  1455. {
  1456. return ring->get_gem_seqno(dev, ring);
  1457. }
  1458. /**
  1459. * This function clears the request list as sequence numbers are passed.
  1460. */
  1461. static void
  1462. i915_gem_retire_requests_ring(struct drm_device *dev,
  1463. struct intel_ring_buffer *ring)
  1464. {
  1465. drm_i915_private_t *dev_priv = dev->dev_private;
  1466. uint32_t seqno;
  1467. if (!ring->status_page.page_addr
  1468. || list_empty(&ring->request_list))
  1469. return;
  1470. seqno = i915_get_gem_seqno(dev, ring);
  1471. while (!list_empty(&ring->request_list)) {
  1472. struct drm_i915_gem_request *request;
  1473. uint32_t retiring_seqno;
  1474. request = list_first_entry(&ring->request_list,
  1475. struct drm_i915_gem_request,
  1476. list);
  1477. retiring_seqno = request->seqno;
  1478. if (i915_seqno_passed(seqno, retiring_seqno) ||
  1479. atomic_read(&dev_priv->mm.wedged)) {
  1480. i915_gem_retire_request(dev, request);
  1481. list_del(&request->list);
  1482. list_del(&request->client_list);
  1483. kfree(request);
  1484. } else
  1485. break;
  1486. }
  1487. if (unlikely (dev_priv->trace_irq_seqno &&
  1488. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1489. ring->user_irq_put(dev, ring);
  1490. dev_priv->trace_irq_seqno = 0;
  1491. }
  1492. }
  1493. void
  1494. i915_gem_retire_requests(struct drm_device *dev)
  1495. {
  1496. drm_i915_private_t *dev_priv = dev->dev_private;
  1497. if (!list_empty(&dev_priv->mm.deferred_free_list)) {
  1498. struct drm_i915_gem_object *obj_priv, *tmp;
  1499. /* We must be careful that during unbind() we do not
  1500. * accidentally infinitely recurse into retire requests.
  1501. * Currently:
  1502. * retire -> free -> unbind -> wait -> retire_ring
  1503. */
  1504. list_for_each_entry_safe(obj_priv, tmp,
  1505. &dev_priv->mm.deferred_free_list,
  1506. list)
  1507. i915_gem_free_object_tail(&obj_priv->base);
  1508. }
  1509. i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
  1510. if (HAS_BSD(dev))
  1511. i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
  1512. }
  1513. static void
  1514. i915_gem_retire_work_handler(struct work_struct *work)
  1515. {
  1516. drm_i915_private_t *dev_priv;
  1517. struct drm_device *dev;
  1518. dev_priv = container_of(work, drm_i915_private_t,
  1519. mm.retire_work.work);
  1520. dev = dev_priv->dev;
  1521. mutex_lock(&dev->struct_mutex);
  1522. i915_gem_retire_requests(dev);
  1523. if (!dev_priv->mm.suspended &&
  1524. (!list_empty(&dev_priv->render_ring.request_list) ||
  1525. (HAS_BSD(dev) &&
  1526. !list_empty(&dev_priv->bsd_ring.request_list))))
  1527. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1528. mutex_unlock(&dev->struct_mutex);
  1529. }
  1530. int
  1531. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1532. bool interruptible, struct intel_ring_buffer *ring)
  1533. {
  1534. drm_i915_private_t *dev_priv = dev->dev_private;
  1535. u32 ier;
  1536. int ret = 0;
  1537. BUG_ON(seqno == 0);
  1538. if (seqno == dev_priv->next_seqno) {
  1539. seqno = i915_add_request(dev, NULL, NULL, ring);
  1540. if (seqno == 0)
  1541. return -ENOMEM;
  1542. }
  1543. if (atomic_read(&dev_priv->mm.wedged))
  1544. return -EIO;
  1545. if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
  1546. if (HAS_PCH_SPLIT(dev))
  1547. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1548. else
  1549. ier = I915_READ(IER);
  1550. if (!ier) {
  1551. DRM_ERROR("something (likely vbetool) disabled "
  1552. "interrupts, re-enabling\n");
  1553. i915_driver_irq_preinstall(dev);
  1554. i915_driver_irq_postinstall(dev);
  1555. }
  1556. trace_i915_gem_request_wait_begin(dev, seqno);
  1557. ring->waiting_gem_seqno = seqno;
  1558. ring->user_irq_get(dev, ring);
  1559. if (interruptible)
  1560. ret = wait_event_interruptible(ring->irq_queue,
  1561. i915_seqno_passed(
  1562. ring->get_gem_seqno(dev, ring), seqno)
  1563. || atomic_read(&dev_priv->mm.wedged));
  1564. else
  1565. wait_event(ring->irq_queue,
  1566. i915_seqno_passed(
  1567. ring->get_gem_seqno(dev, ring), seqno)
  1568. || atomic_read(&dev_priv->mm.wedged));
  1569. ring->user_irq_put(dev, ring);
  1570. ring->waiting_gem_seqno = 0;
  1571. trace_i915_gem_request_wait_end(dev, seqno);
  1572. }
  1573. if (atomic_read(&dev_priv->mm.wedged))
  1574. ret = -EIO;
  1575. if (ret && ret != -ERESTARTSYS)
  1576. DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
  1577. __func__, ret, seqno, ring->get_gem_seqno(dev, ring),
  1578. dev_priv->next_seqno);
  1579. /* Directly dispatch request retiring. While we have the work queue
  1580. * to handle this, the waiter on a request often wants an associated
  1581. * buffer to have made it to the inactive list, and we would need
  1582. * a separate wait queue to handle that.
  1583. */
  1584. if (ret == 0)
  1585. i915_gem_retire_requests_ring(dev, ring);
  1586. return ret;
  1587. }
  1588. /**
  1589. * Waits for a sequence number to be signaled, and cleans up the
  1590. * request and object lists appropriately for that event.
  1591. */
  1592. static int
  1593. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1594. struct intel_ring_buffer *ring)
  1595. {
  1596. return i915_do_wait_request(dev, seqno, 1, ring);
  1597. }
  1598. static void
  1599. i915_gem_flush(struct drm_device *dev,
  1600. uint32_t invalidate_domains,
  1601. uint32_t flush_domains)
  1602. {
  1603. drm_i915_private_t *dev_priv = dev->dev_private;
  1604. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1605. drm_agp_chipset_flush(dev);
  1606. dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
  1607. invalidate_domains,
  1608. flush_domains);
  1609. if (HAS_BSD(dev))
  1610. dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
  1611. invalidate_domains,
  1612. flush_domains);
  1613. }
  1614. /**
  1615. * Ensures that all rendering to the object has completed and the object is
  1616. * safe to unbind from the GTT or access from the CPU.
  1617. */
  1618. static int
  1619. i915_gem_object_wait_rendering(struct drm_gem_object *obj,
  1620. bool interruptible)
  1621. {
  1622. struct drm_device *dev = obj->dev;
  1623. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1624. int ret;
  1625. /* This function only exists to support waiting for existing rendering,
  1626. * not for emitting required flushes.
  1627. */
  1628. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1629. /* If there is rendering queued on the buffer being evicted, wait for
  1630. * it.
  1631. */
  1632. if (obj_priv->active) {
  1633. #if WATCH_BUF
  1634. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1635. __func__, obj, obj_priv->last_rendering_seqno);
  1636. #endif
  1637. ret = i915_do_wait_request(dev,
  1638. obj_priv->last_rendering_seqno,
  1639. interruptible,
  1640. obj_priv->ring);
  1641. if (ret)
  1642. return ret;
  1643. }
  1644. return 0;
  1645. }
  1646. /**
  1647. * Unbinds an object from the GTT aperture.
  1648. */
  1649. int
  1650. i915_gem_object_unbind(struct drm_gem_object *obj)
  1651. {
  1652. struct drm_device *dev = obj->dev;
  1653. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1654. int ret = 0;
  1655. #if WATCH_BUF
  1656. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1657. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1658. #endif
  1659. if (obj_priv->gtt_space == NULL)
  1660. return 0;
  1661. if (obj_priv->pin_count != 0) {
  1662. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1663. return -EINVAL;
  1664. }
  1665. /* blow away mappings if mapped through GTT */
  1666. i915_gem_release_mmap(obj);
  1667. /* Move the object to the CPU domain to ensure that
  1668. * any possible CPU writes while it's not in the GTT
  1669. * are flushed when we go to remap it. This will
  1670. * also ensure that all pending GPU writes are finished
  1671. * before we unbind.
  1672. */
  1673. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1674. if (ret == -ERESTARTSYS)
  1675. return ret;
  1676. /* Continue on if we fail due to EIO, the GPU is hung so we
  1677. * should be safe and we need to cleanup or else we might
  1678. * cause memory corruption through use-after-free.
  1679. */
  1680. /* release the fence reg _after_ flushing */
  1681. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1682. i915_gem_clear_fence_reg(obj);
  1683. if (obj_priv->agp_mem != NULL) {
  1684. drm_unbind_agp(obj_priv->agp_mem);
  1685. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1686. obj_priv->agp_mem = NULL;
  1687. }
  1688. i915_gem_object_put_pages(obj);
  1689. BUG_ON(obj_priv->pages_refcount);
  1690. if (obj_priv->gtt_space) {
  1691. atomic_dec(&dev->gtt_count);
  1692. atomic_sub(obj->size, &dev->gtt_memory);
  1693. drm_mm_put_block(obj_priv->gtt_space);
  1694. obj_priv->gtt_space = NULL;
  1695. }
  1696. /* Remove ourselves from the LRU list if present. */
  1697. if (!list_empty(&obj_priv->list))
  1698. list_del_init(&obj_priv->list);
  1699. if (i915_gem_object_is_purgeable(obj_priv))
  1700. i915_gem_object_truncate(obj);
  1701. trace_i915_gem_object_unbind(obj);
  1702. return ret;
  1703. }
  1704. int
  1705. i915_gpu_idle(struct drm_device *dev)
  1706. {
  1707. drm_i915_private_t *dev_priv = dev->dev_private;
  1708. bool lists_empty;
  1709. int ret;
  1710. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1711. list_empty(&dev_priv->render_ring.active_list) &&
  1712. (!HAS_BSD(dev) ||
  1713. list_empty(&dev_priv->bsd_ring.active_list)));
  1714. if (lists_empty)
  1715. return 0;
  1716. /* Flush everything onto the inactive list. */
  1717. i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1718. ret = i915_wait_request(dev,
  1719. i915_gem_next_request_seqno(dev, &dev_priv->render_ring),
  1720. &dev_priv->render_ring);
  1721. if (ret)
  1722. return ret;
  1723. if (HAS_BSD(dev)) {
  1724. ret = i915_wait_request(dev,
  1725. i915_gem_next_request_seqno(dev, &dev_priv->bsd_ring),
  1726. &dev_priv->bsd_ring);
  1727. if (ret)
  1728. return ret;
  1729. }
  1730. return 0;
  1731. }
  1732. int
  1733. i915_gem_object_get_pages(struct drm_gem_object *obj,
  1734. gfp_t gfpmask)
  1735. {
  1736. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1737. int page_count, i;
  1738. struct address_space *mapping;
  1739. struct inode *inode;
  1740. struct page *page;
  1741. BUG_ON(obj_priv->pages_refcount
  1742. == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
  1743. if (obj_priv->pages_refcount++ != 0)
  1744. return 0;
  1745. /* Get the list of pages out of our struct file. They'll be pinned
  1746. * at this point until we release them.
  1747. */
  1748. page_count = obj->size / PAGE_SIZE;
  1749. BUG_ON(obj_priv->pages != NULL);
  1750. obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
  1751. if (obj_priv->pages == NULL) {
  1752. obj_priv->pages_refcount--;
  1753. return -ENOMEM;
  1754. }
  1755. inode = obj->filp->f_path.dentry->d_inode;
  1756. mapping = inode->i_mapping;
  1757. for (i = 0; i < page_count; i++) {
  1758. page = read_cache_page_gfp(mapping, i,
  1759. GFP_HIGHUSER |
  1760. __GFP_COLD |
  1761. __GFP_RECLAIMABLE |
  1762. gfpmask);
  1763. if (IS_ERR(page))
  1764. goto err_pages;
  1765. obj_priv->pages[i] = page;
  1766. }
  1767. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1768. i915_gem_object_do_bit_17_swizzle(obj);
  1769. return 0;
  1770. err_pages:
  1771. while (i--)
  1772. page_cache_release(obj_priv->pages[i]);
  1773. drm_free_large(obj_priv->pages);
  1774. obj_priv->pages = NULL;
  1775. obj_priv->pages_refcount--;
  1776. return PTR_ERR(page);
  1777. }
  1778. static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
  1779. {
  1780. struct drm_gem_object *obj = reg->obj;
  1781. struct drm_device *dev = obj->dev;
  1782. drm_i915_private_t *dev_priv = dev->dev_private;
  1783. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1784. int regnum = obj_priv->fence_reg;
  1785. uint64_t val;
  1786. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1787. 0xfffff000) << 32;
  1788. val |= obj_priv->gtt_offset & 0xfffff000;
  1789. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1790. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1791. if (obj_priv->tiling_mode == I915_TILING_Y)
  1792. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1793. val |= I965_FENCE_REG_VALID;
  1794. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1795. }
  1796. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1797. {
  1798. struct drm_gem_object *obj = reg->obj;
  1799. struct drm_device *dev = obj->dev;
  1800. drm_i915_private_t *dev_priv = dev->dev_private;
  1801. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1802. int regnum = obj_priv->fence_reg;
  1803. uint64_t val;
  1804. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1805. 0xfffff000) << 32;
  1806. val |= obj_priv->gtt_offset & 0xfffff000;
  1807. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1808. if (obj_priv->tiling_mode == I915_TILING_Y)
  1809. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1810. val |= I965_FENCE_REG_VALID;
  1811. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1812. }
  1813. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1814. {
  1815. struct drm_gem_object *obj = reg->obj;
  1816. struct drm_device *dev = obj->dev;
  1817. drm_i915_private_t *dev_priv = dev->dev_private;
  1818. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1819. int regnum = obj_priv->fence_reg;
  1820. int tile_width;
  1821. uint32_t fence_reg, val;
  1822. uint32_t pitch_val;
  1823. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1824. (obj_priv->gtt_offset & (obj->size - 1))) {
  1825. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1826. __func__, obj_priv->gtt_offset, obj->size);
  1827. return;
  1828. }
  1829. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1830. HAS_128_BYTE_Y_TILING(dev))
  1831. tile_width = 128;
  1832. else
  1833. tile_width = 512;
  1834. /* Note: pitch better be a power of two tile widths */
  1835. pitch_val = obj_priv->stride / tile_width;
  1836. pitch_val = ffs(pitch_val) - 1;
  1837. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1838. HAS_128_BYTE_Y_TILING(dev))
  1839. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1840. else
  1841. WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
  1842. val = obj_priv->gtt_offset;
  1843. if (obj_priv->tiling_mode == I915_TILING_Y)
  1844. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1845. val |= I915_FENCE_SIZE_BITS(obj->size);
  1846. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1847. val |= I830_FENCE_REG_VALID;
  1848. if (regnum < 8)
  1849. fence_reg = FENCE_REG_830_0 + (regnum * 4);
  1850. else
  1851. fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
  1852. I915_WRITE(fence_reg, val);
  1853. }
  1854. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1855. {
  1856. struct drm_gem_object *obj = reg->obj;
  1857. struct drm_device *dev = obj->dev;
  1858. drm_i915_private_t *dev_priv = dev->dev_private;
  1859. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1860. int regnum = obj_priv->fence_reg;
  1861. uint32_t val;
  1862. uint32_t pitch_val;
  1863. uint32_t fence_size_bits;
  1864. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  1865. (obj_priv->gtt_offset & (obj->size - 1))) {
  1866. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  1867. __func__, obj_priv->gtt_offset);
  1868. return;
  1869. }
  1870. pitch_val = obj_priv->stride / 128;
  1871. pitch_val = ffs(pitch_val) - 1;
  1872. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1873. val = obj_priv->gtt_offset;
  1874. if (obj_priv->tiling_mode == I915_TILING_Y)
  1875. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1876. fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
  1877. WARN_ON(fence_size_bits & ~0x00000f00);
  1878. val |= fence_size_bits;
  1879. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1880. val |= I830_FENCE_REG_VALID;
  1881. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1882. }
  1883. static int i915_find_fence_reg(struct drm_device *dev,
  1884. bool interruptible)
  1885. {
  1886. struct drm_i915_fence_reg *reg = NULL;
  1887. struct drm_i915_gem_object *obj_priv = NULL;
  1888. struct drm_i915_private *dev_priv = dev->dev_private;
  1889. struct drm_gem_object *obj = NULL;
  1890. int i, avail, ret;
  1891. /* First try to find a free reg */
  1892. avail = 0;
  1893. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1894. reg = &dev_priv->fence_regs[i];
  1895. if (!reg->obj)
  1896. return i;
  1897. obj_priv = to_intel_bo(reg->obj);
  1898. if (!obj_priv->pin_count)
  1899. avail++;
  1900. }
  1901. if (avail == 0)
  1902. return -ENOSPC;
  1903. /* None available, try to steal one or wait for a user to finish */
  1904. i = I915_FENCE_REG_NONE;
  1905. list_for_each_entry(reg, &dev_priv->mm.fence_list,
  1906. lru_list) {
  1907. obj = reg->obj;
  1908. obj_priv = to_intel_bo(obj);
  1909. if (obj_priv->pin_count)
  1910. continue;
  1911. /* found one! */
  1912. i = obj_priv->fence_reg;
  1913. break;
  1914. }
  1915. BUG_ON(i == I915_FENCE_REG_NONE);
  1916. /* We only have a reference on obj from the active list. put_fence_reg
  1917. * might drop that one, causing a use-after-free in it. So hold a
  1918. * private reference to obj like the other callers of put_fence_reg
  1919. * (set_tiling ioctl) do. */
  1920. drm_gem_object_reference(obj);
  1921. ret = i915_gem_object_put_fence_reg(obj, interruptible);
  1922. drm_gem_object_unreference(obj);
  1923. if (ret != 0)
  1924. return ret;
  1925. return i;
  1926. }
  1927. /**
  1928. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  1929. * @obj: object to map through a fence reg
  1930. *
  1931. * When mapping objects through the GTT, userspace wants to be able to write
  1932. * to them without having to worry about swizzling if the object is tiled.
  1933. *
  1934. * This function walks the fence regs looking for a free one for @obj,
  1935. * stealing one if it can't find any.
  1936. *
  1937. * It then sets up the reg based on the object's properties: address, pitch
  1938. * and tiling format.
  1939. */
  1940. int
  1941. i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
  1942. bool interruptible)
  1943. {
  1944. struct drm_device *dev = obj->dev;
  1945. struct drm_i915_private *dev_priv = dev->dev_private;
  1946. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1947. struct drm_i915_fence_reg *reg = NULL;
  1948. int ret;
  1949. /* Just update our place in the LRU if our fence is getting used. */
  1950. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1951. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  1952. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  1953. return 0;
  1954. }
  1955. switch (obj_priv->tiling_mode) {
  1956. case I915_TILING_NONE:
  1957. WARN(1, "allocating a fence for non-tiled object?\n");
  1958. break;
  1959. case I915_TILING_X:
  1960. if (!obj_priv->stride)
  1961. return -EINVAL;
  1962. WARN((obj_priv->stride & (512 - 1)),
  1963. "object 0x%08x is X tiled but has non-512B pitch\n",
  1964. obj_priv->gtt_offset);
  1965. break;
  1966. case I915_TILING_Y:
  1967. if (!obj_priv->stride)
  1968. return -EINVAL;
  1969. WARN((obj_priv->stride & (128 - 1)),
  1970. "object 0x%08x is Y tiled but has non-128B pitch\n",
  1971. obj_priv->gtt_offset);
  1972. break;
  1973. }
  1974. ret = i915_find_fence_reg(dev, interruptible);
  1975. if (ret < 0)
  1976. return ret;
  1977. obj_priv->fence_reg = ret;
  1978. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  1979. list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  1980. reg->obj = obj;
  1981. if (IS_GEN6(dev))
  1982. sandybridge_write_fence_reg(reg);
  1983. else if (IS_I965G(dev))
  1984. i965_write_fence_reg(reg);
  1985. else if (IS_I9XX(dev))
  1986. i915_write_fence_reg(reg);
  1987. else
  1988. i830_write_fence_reg(reg);
  1989. trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
  1990. obj_priv->tiling_mode);
  1991. return 0;
  1992. }
  1993. /**
  1994. * i915_gem_clear_fence_reg - clear out fence register info
  1995. * @obj: object to clear
  1996. *
  1997. * Zeroes out the fence register itself and clears out the associated
  1998. * data structures in dev_priv and obj_priv.
  1999. */
  2000. static void
  2001. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  2002. {
  2003. struct drm_device *dev = obj->dev;
  2004. drm_i915_private_t *dev_priv = dev->dev_private;
  2005. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2006. struct drm_i915_fence_reg *reg =
  2007. &dev_priv->fence_regs[obj_priv->fence_reg];
  2008. if (IS_GEN6(dev)) {
  2009. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2010. (obj_priv->fence_reg * 8), 0);
  2011. } else if (IS_I965G(dev)) {
  2012. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2013. } else {
  2014. uint32_t fence_reg;
  2015. if (obj_priv->fence_reg < 8)
  2016. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2017. else
  2018. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
  2019. 8) * 4;
  2020. I915_WRITE(fence_reg, 0);
  2021. }
  2022. reg->obj = NULL;
  2023. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2024. list_del_init(&reg->lru_list);
  2025. }
  2026. /**
  2027. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2028. * to the buffer to finish, and then resets the fence register.
  2029. * @obj: tiled object holding a fence register.
  2030. * @bool: whether the wait upon the fence is interruptible
  2031. *
  2032. * Zeroes out the fence register itself and clears out the associated
  2033. * data structures in dev_priv and obj_priv.
  2034. */
  2035. int
  2036. i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
  2037. bool interruptible)
  2038. {
  2039. struct drm_device *dev = obj->dev;
  2040. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2041. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2042. return 0;
  2043. /* If we've changed tiling, GTT-mappings of the object
  2044. * need to re-fault to ensure that the correct fence register
  2045. * setup is in place.
  2046. */
  2047. i915_gem_release_mmap(obj);
  2048. /* On the i915, GPU access to tiled buffers is via a fence,
  2049. * therefore we must wait for any outstanding access to complete
  2050. * before clearing the fence.
  2051. */
  2052. if (!IS_I965G(dev)) {
  2053. int ret;
  2054. ret = i915_gem_object_flush_gpu_write_domain(obj, true);
  2055. if (ret)
  2056. return ret;
  2057. ret = i915_gem_object_wait_rendering(obj, interruptible);
  2058. if (ret)
  2059. return ret;
  2060. }
  2061. i915_gem_object_flush_gtt_write_domain(obj);
  2062. i915_gem_clear_fence_reg(obj);
  2063. return 0;
  2064. }
  2065. /**
  2066. * Finds free space in the GTT aperture and binds the object there.
  2067. */
  2068. static int
  2069. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  2070. {
  2071. struct drm_device *dev = obj->dev;
  2072. drm_i915_private_t *dev_priv = dev->dev_private;
  2073. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2074. struct drm_mm_node *free_space;
  2075. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2076. int ret;
  2077. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2078. DRM_ERROR("Attempting to bind a purgeable object\n");
  2079. return -EINVAL;
  2080. }
  2081. if (alignment == 0)
  2082. alignment = i915_gem_get_gtt_alignment(obj);
  2083. if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
  2084. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2085. return -EINVAL;
  2086. }
  2087. /* If the object is bigger than the entire aperture, reject it early
  2088. * before evicting everything in a vain attempt to find space.
  2089. */
  2090. if (obj->size > dev->gtt_total) {
  2091. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2092. return -E2BIG;
  2093. }
  2094. search_free:
  2095. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2096. obj->size, alignment, 0);
  2097. if (free_space != NULL) {
  2098. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  2099. alignment);
  2100. if (obj_priv->gtt_space != NULL)
  2101. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2102. }
  2103. if (obj_priv->gtt_space == NULL) {
  2104. /* If the gtt is empty and we're still having trouble
  2105. * fitting our object in, we're out of memory.
  2106. */
  2107. #if WATCH_LRU
  2108. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  2109. #endif
  2110. ret = i915_gem_evict_something(dev, obj->size, alignment);
  2111. if (ret)
  2112. return ret;
  2113. goto search_free;
  2114. }
  2115. #if WATCH_BUF
  2116. DRM_INFO("Binding object of size %zd at 0x%08x\n",
  2117. obj->size, obj_priv->gtt_offset);
  2118. #endif
  2119. ret = i915_gem_object_get_pages(obj, gfpmask);
  2120. if (ret) {
  2121. drm_mm_put_block(obj_priv->gtt_space);
  2122. obj_priv->gtt_space = NULL;
  2123. if (ret == -ENOMEM) {
  2124. /* first try to clear up some space from the GTT */
  2125. ret = i915_gem_evict_something(dev, obj->size,
  2126. alignment);
  2127. if (ret) {
  2128. /* now try to shrink everyone else */
  2129. if (gfpmask) {
  2130. gfpmask = 0;
  2131. goto search_free;
  2132. }
  2133. return ret;
  2134. }
  2135. goto search_free;
  2136. }
  2137. return ret;
  2138. }
  2139. /* Create an AGP memory structure pointing at our pages, and bind it
  2140. * into the GTT.
  2141. */
  2142. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2143. obj_priv->pages,
  2144. obj->size >> PAGE_SHIFT,
  2145. obj_priv->gtt_offset,
  2146. obj_priv->agp_type);
  2147. if (obj_priv->agp_mem == NULL) {
  2148. i915_gem_object_put_pages(obj);
  2149. drm_mm_put_block(obj_priv->gtt_space);
  2150. obj_priv->gtt_space = NULL;
  2151. ret = i915_gem_evict_something(dev, obj->size, alignment);
  2152. if (ret)
  2153. return ret;
  2154. goto search_free;
  2155. }
  2156. atomic_inc(&dev->gtt_count);
  2157. atomic_add(obj->size, &dev->gtt_memory);
  2158. /* keep track of bounds object by adding it to the inactive list */
  2159. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  2160. /* Assert that the object is not currently in any GPU domain. As it
  2161. * wasn't in the GTT, there shouldn't be any way it could have been in
  2162. * a GPU cache
  2163. */
  2164. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2165. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2166. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
  2167. return 0;
  2168. }
  2169. void
  2170. i915_gem_clflush_object(struct drm_gem_object *obj)
  2171. {
  2172. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2173. /* If we don't have a page list set up, then we're not pinned
  2174. * to GPU, and we can ignore the cache flush because it'll happen
  2175. * again at bind time.
  2176. */
  2177. if (obj_priv->pages == NULL)
  2178. return;
  2179. trace_i915_gem_object_clflush(obj);
  2180. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2181. }
  2182. /** Flushes any GPU write domain for the object if it's dirty. */
  2183. static int
  2184. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
  2185. bool pipelined)
  2186. {
  2187. struct drm_device *dev = obj->dev;
  2188. uint32_t old_write_domain;
  2189. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2190. return 0;
  2191. /* Queue the GPU write cache flushing we need. */
  2192. old_write_domain = obj->write_domain;
  2193. i915_gem_flush(dev, 0, obj->write_domain);
  2194. BUG_ON(obj->write_domain);
  2195. trace_i915_gem_object_change_domain(obj,
  2196. obj->read_domains,
  2197. old_write_domain);
  2198. if (pipelined)
  2199. return 0;
  2200. return i915_gem_object_wait_rendering(obj, true);
  2201. }
  2202. /** Flushes the GTT write domain for the object if it's dirty. */
  2203. static void
  2204. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2205. {
  2206. uint32_t old_write_domain;
  2207. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2208. return;
  2209. /* No actual flushing is required for the GTT write domain. Writes
  2210. * to it immediately go to main memory as far as we know, so there's
  2211. * no chipset flush. It also doesn't land in render cache.
  2212. */
  2213. old_write_domain = obj->write_domain;
  2214. obj->write_domain = 0;
  2215. trace_i915_gem_object_change_domain(obj,
  2216. obj->read_domains,
  2217. old_write_domain);
  2218. }
  2219. /** Flushes the CPU write domain for the object if it's dirty. */
  2220. static void
  2221. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2222. {
  2223. struct drm_device *dev = obj->dev;
  2224. uint32_t old_write_domain;
  2225. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2226. return;
  2227. i915_gem_clflush_object(obj);
  2228. drm_agp_chipset_flush(dev);
  2229. old_write_domain = obj->write_domain;
  2230. obj->write_domain = 0;
  2231. trace_i915_gem_object_change_domain(obj,
  2232. obj->read_domains,
  2233. old_write_domain);
  2234. }
  2235. /**
  2236. * Moves a single object to the GTT read, and possibly write domain.
  2237. *
  2238. * This function returns when the move is complete, including waiting on
  2239. * flushes to occur.
  2240. */
  2241. int
  2242. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2243. {
  2244. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2245. uint32_t old_write_domain, old_read_domains;
  2246. int ret;
  2247. /* Not valid to be called on unbound objects. */
  2248. if (obj_priv->gtt_space == NULL)
  2249. return -EINVAL;
  2250. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2251. if (ret != 0)
  2252. return ret;
  2253. i915_gem_object_flush_cpu_write_domain(obj);
  2254. if (write) {
  2255. ret = i915_gem_object_wait_rendering(obj, true);
  2256. if (ret)
  2257. return ret;
  2258. }
  2259. old_write_domain = obj->write_domain;
  2260. old_read_domains = obj->read_domains;
  2261. /* It should now be out of any other write domains, and we can update
  2262. * the domain values for our changes.
  2263. */
  2264. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2265. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2266. if (write) {
  2267. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2268. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2269. obj_priv->dirty = 1;
  2270. }
  2271. trace_i915_gem_object_change_domain(obj,
  2272. old_read_domains,
  2273. old_write_domain);
  2274. return 0;
  2275. }
  2276. /*
  2277. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2278. * wait, as in modesetting process we're not supposed to be interrupted.
  2279. */
  2280. int
  2281. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
  2282. bool pipelined)
  2283. {
  2284. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2285. uint32_t old_read_domains;
  2286. int ret;
  2287. /* Not valid to be called on unbound objects. */
  2288. if (obj_priv->gtt_space == NULL)
  2289. return -EINVAL;
  2290. ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
  2291. if (ret)
  2292. return ret;
  2293. i915_gem_object_flush_cpu_write_domain(obj);
  2294. old_read_domains = obj->read_domains;
  2295. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2296. trace_i915_gem_object_change_domain(obj,
  2297. old_read_domains,
  2298. obj->write_domain);
  2299. return 0;
  2300. }
  2301. /**
  2302. * Moves a single object to the CPU read, and possibly write domain.
  2303. *
  2304. * This function returns when the move is complete, including waiting on
  2305. * flushes to occur.
  2306. */
  2307. static int
  2308. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2309. {
  2310. uint32_t old_write_domain, old_read_domains;
  2311. int ret;
  2312. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2313. if (ret != 0)
  2314. return ret;
  2315. i915_gem_object_flush_gtt_write_domain(obj);
  2316. /* If we have a partially-valid cache of the object in the CPU,
  2317. * finish invalidating it and free the per-page flags.
  2318. */
  2319. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2320. if (write) {
  2321. ret = i915_gem_object_wait_rendering(obj, true);
  2322. if (ret)
  2323. return ret;
  2324. }
  2325. old_write_domain = obj->write_domain;
  2326. old_read_domains = obj->read_domains;
  2327. /* Flush the CPU cache if it's still invalid. */
  2328. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2329. i915_gem_clflush_object(obj);
  2330. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2331. }
  2332. /* It should now be out of any other write domains, and we can update
  2333. * the domain values for our changes.
  2334. */
  2335. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2336. /* If we're writing through the CPU, then the GPU read domains will
  2337. * need to be invalidated at next use.
  2338. */
  2339. if (write) {
  2340. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  2341. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2342. }
  2343. trace_i915_gem_object_change_domain(obj,
  2344. old_read_domains,
  2345. old_write_domain);
  2346. return 0;
  2347. }
  2348. /*
  2349. * Set the next domain for the specified object. This
  2350. * may not actually perform the necessary flushing/invaliding though,
  2351. * as that may want to be batched with other set_domain operations
  2352. *
  2353. * This is (we hope) the only really tricky part of gem. The goal
  2354. * is fairly simple -- track which caches hold bits of the object
  2355. * and make sure they remain coherent. A few concrete examples may
  2356. * help to explain how it works. For shorthand, we use the notation
  2357. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2358. * a pair of read and write domain masks.
  2359. *
  2360. * Case 1: the batch buffer
  2361. *
  2362. * 1. Allocated
  2363. * 2. Written by CPU
  2364. * 3. Mapped to GTT
  2365. * 4. Read by GPU
  2366. * 5. Unmapped from GTT
  2367. * 6. Freed
  2368. *
  2369. * Let's take these a step at a time
  2370. *
  2371. * 1. Allocated
  2372. * Pages allocated from the kernel may still have
  2373. * cache contents, so we set them to (CPU, CPU) always.
  2374. * 2. Written by CPU (using pwrite)
  2375. * The pwrite function calls set_domain (CPU, CPU) and
  2376. * this function does nothing (as nothing changes)
  2377. * 3. Mapped by GTT
  2378. * This function asserts that the object is not
  2379. * currently in any GPU-based read or write domains
  2380. * 4. Read by GPU
  2381. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2382. * As write_domain is zero, this function adds in the
  2383. * current read domains (CPU+COMMAND, 0).
  2384. * flush_domains is set to CPU.
  2385. * invalidate_domains is set to COMMAND
  2386. * clflush is run to get data out of the CPU caches
  2387. * then i915_dev_set_domain calls i915_gem_flush to
  2388. * emit an MI_FLUSH and drm_agp_chipset_flush
  2389. * 5. Unmapped from GTT
  2390. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2391. * flush_domains and invalidate_domains end up both zero
  2392. * so no flushing/invalidating happens
  2393. * 6. Freed
  2394. * yay, done
  2395. *
  2396. * Case 2: The shared render buffer
  2397. *
  2398. * 1. Allocated
  2399. * 2. Mapped to GTT
  2400. * 3. Read/written by GPU
  2401. * 4. set_domain to (CPU,CPU)
  2402. * 5. Read/written by CPU
  2403. * 6. Read/written by GPU
  2404. *
  2405. * 1. Allocated
  2406. * Same as last example, (CPU, CPU)
  2407. * 2. Mapped to GTT
  2408. * Nothing changes (assertions find that it is not in the GPU)
  2409. * 3. Read/written by GPU
  2410. * execbuffer calls set_domain (RENDER, RENDER)
  2411. * flush_domains gets CPU
  2412. * invalidate_domains gets GPU
  2413. * clflush (obj)
  2414. * MI_FLUSH and drm_agp_chipset_flush
  2415. * 4. set_domain (CPU, CPU)
  2416. * flush_domains gets GPU
  2417. * invalidate_domains gets CPU
  2418. * wait_rendering (obj) to make sure all drawing is complete.
  2419. * This will include an MI_FLUSH to get the data from GPU
  2420. * to memory
  2421. * clflush (obj) to invalidate the CPU cache
  2422. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2423. * 5. Read/written by CPU
  2424. * cache lines are loaded and dirtied
  2425. * 6. Read written by GPU
  2426. * Same as last GPU access
  2427. *
  2428. * Case 3: The constant buffer
  2429. *
  2430. * 1. Allocated
  2431. * 2. Written by CPU
  2432. * 3. Read by GPU
  2433. * 4. Updated (written) by CPU again
  2434. * 5. Read by GPU
  2435. *
  2436. * 1. Allocated
  2437. * (CPU, CPU)
  2438. * 2. Written by CPU
  2439. * (CPU, CPU)
  2440. * 3. Read by GPU
  2441. * (CPU+RENDER, 0)
  2442. * flush_domains = CPU
  2443. * invalidate_domains = RENDER
  2444. * clflush (obj)
  2445. * MI_FLUSH
  2446. * drm_agp_chipset_flush
  2447. * 4. Updated (written) by CPU again
  2448. * (CPU, CPU)
  2449. * flush_domains = 0 (no previous write domain)
  2450. * invalidate_domains = 0 (no new read domains)
  2451. * 5. Read by GPU
  2452. * (CPU+RENDER, 0)
  2453. * flush_domains = CPU
  2454. * invalidate_domains = RENDER
  2455. * clflush (obj)
  2456. * MI_FLUSH
  2457. * drm_agp_chipset_flush
  2458. */
  2459. static void
  2460. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  2461. {
  2462. struct drm_device *dev = obj->dev;
  2463. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2464. uint32_t invalidate_domains = 0;
  2465. uint32_t flush_domains = 0;
  2466. uint32_t old_read_domains;
  2467. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  2468. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  2469. intel_mark_busy(dev, obj);
  2470. #if WATCH_BUF
  2471. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  2472. __func__, obj,
  2473. obj->read_domains, obj->pending_read_domains,
  2474. obj->write_domain, obj->pending_write_domain);
  2475. #endif
  2476. /*
  2477. * If the object isn't moving to a new write domain,
  2478. * let the object stay in multiple read domains
  2479. */
  2480. if (obj->pending_write_domain == 0)
  2481. obj->pending_read_domains |= obj->read_domains;
  2482. else
  2483. obj_priv->dirty = 1;
  2484. /*
  2485. * Flush the current write domain if
  2486. * the new read domains don't match. Invalidate
  2487. * any read domains which differ from the old
  2488. * write domain
  2489. */
  2490. if (obj->write_domain &&
  2491. obj->write_domain != obj->pending_read_domains) {
  2492. flush_domains |= obj->write_domain;
  2493. invalidate_domains |=
  2494. obj->pending_read_domains & ~obj->write_domain;
  2495. }
  2496. /*
  2497. * Invalidate any read caches which may have
  2498. * stale data. That is, any new read domains.
  2499. */
  2500. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2501. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  2502. #if WATCH_BUF
  2503. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  2504. __func__, flush_domains, invalidate_domains);
  2505. #endif
  2506. i915_gem_clflush_object(obj);
  2507. }
  2508. old_read_domains = obj->read_domains;
  2509. /* The actual obj->write_domain will be updated with
  2510. * pending_write_domain after we emit the accumulated flush for all
  2511. * of our domain changes in execbuffers (which clears objects'
  2512. * write_domains). So if we have a current write domain that we
  2513. * aren't changing, set pending_write_domain to that.
  2514. */
  2515. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2516. obj->pending_write_domain = obj->write_domain;
  2517. obj->read_domains = obj->pending_read_domains;
  2518. dev->invalidate_domains |= invalidate_domains;
  2519. dev->flush_domains |= flush_domains;
  2520. #if WATCH_BUF
  2521. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  2522. __func__,
  2523. obj->read_domains, obj->write_domain,
  2524. dev->invalidate_domains, dev->flush_domains);
  2525. #endif
  2526. trace_i915_gem_object_change_domain(obj,
  2527. old_read_domains,
  2528. obj->write_domain);
  2529. }
  2530. /**
  2531. * Moves the object from a partially CPU read to a full one.
  2532. *
  2533. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2534. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2535. */
  2536. static void
  2537. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2538. {
  2539. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2540. if (!obj_priv->page_cpu_valid)
  2541. return;
  2542. /* If we're partially in the CPU read domain, finish moving it in.
  2543. */
  2544. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2545. int i;
  2546. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2547. if (obj_priv->page_cpu_valid[i])
  2548. continue;
  2549. drm_clflush_pages(obj_priv->pages + i, 1);
  2550. }
  2551. }
  2552. /* Free the page_cpu_valid mappings which are now stale, whether
  2553. * or not we've got I915_GEM_DOMAIN_CPU.
  2554. */
  2555. kfree(obj_priv->page_cpu_valid);
  2556. obj_priv->page_cpu_valid = NULL;
  2557. }
  2558. /**
  2559. * Set the CPU read domain on a range of the object.
  2560. *
  2561. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2562. * not entirely valid. The page_cpu_valid member of the object flags which
  2563. * pages have been flushed, and will be respected by
  2564. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2565. * of the whole object.
  2566. *
  2567. * This function returns when the move is complete, including waiting on
  2568. * flushes to occur.
  2569. */
  2570. static int
  2571. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2572. uint64_t offset, uint64_t size)
  2573. {
  2574. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2575. uint32_t old_read_domains;
  2576. int i, ret;
  2577. if (offset == 0 && size == obj->size)
  2578. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2579. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2580. if (ret != 0)
  2581. return ret;
  2582. i915_gem_object_flush_gtt_write_domain(obj);
  2583. /* If we're already fully in the CPU read domain, we're done. */
  2584. if (obj_priv->page_cpu_valid == NULL &&
  2585. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2586. return 0;
  2587. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2588. * newly adding I915_GEM_DOMAIN_CPU
  2589. */
  2590. if (obj_priv->page_cpu_valid == NULL) {
  2591. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2592. GFP_KERNEL);
  2593. if (obj_priv->page_cpu_valid == NULL)
  2594. return -ENOMEM;
  2595. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2596. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2597. /* Flush the cache on any pages that are still invalid from the CPU's
  2598. * perspective.
  2599. */
  2600. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2601. i++) {
  2602. if (obj_priv->page_cpu_valid[i])
  2603. continue;
  2604. drm_clflush_pages(obj_priv->pages + i, 1);
  2605. obj_priv->page_cpu_valid[i] = 1;
  2606. }
  2607. /* It should now be out of any other write domains, and we can update
  2608. * the domain values for our changes.
  2609. */
  2610. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2611. old_read_domains = obj->read_domains;
  2612. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2613. trace_i915_gem_object_change_domain(obj,
  2614. old_read_domains,
  2615. obj->write_domain);
  2616. return 0;
  2617. }
  2618. /**
  2619. * Pin an object to the GTT and evaluate the relocations landing in it.
  2620. */
  2621. static int
  2622. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  2623. struct drm_file *file_priv,
  2624. struct drm_i915_gem_exec_object2 *entry,
  2625. struct drm_i915_gem_relocation_entry *relocs)
  2626. {
  2627. struct drm_device *dev = obj->dev;
  2628. drm_i915_private_t *dev_priv = dev->dev_private;
  2629. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2630. int i, ret;
  2631. void __iomem *reloc_page;
  2632. bool need_fence;
  2633. need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  2634. obj_priv->tiling_mode != I915_TILING_NONE;
  2635. /* Check fence reg constraints and rebind if necessary */
  2636. if (need_fence &&
  2637. !i915_gem_object_fence_offset_ok(obj,
  2638. obj_priv->tiling_mode)) {
  2639. ret = i915_gem_object_unbind(obj);
  2640. if (ret)
  2641. return ret;
  2642. }
  2643. /* Choose the GTT offset for our buffer and put it there. */
  2644. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  2645. if (ret)
  2646. return ret;
  2647. /*
  2648. * Pre-965 chips need a fence register set up in order to
  2649. * properly handle blits to/from tiled surfaces.
  2650. */
  2651. if (need_fence) {
  2652. ret = i915_gem_object_get_fence_reg(obj, false);
  2653. if (ret != 0) {
  2654. i915_gem_object_unpin(obj);
  2655. return ret;
  2656. }
  2657. }
  2658. entry->offset = obj_priv->gtt_offset;
  2659. /* Apply the relocations, using the GTT aperture to avoid cache
  2660. * flushing requirements.
  2661. */
  2662. for (i = 0; i < entry->relocation_count; i++) {
  2663. struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
  2664. struct drm_gem_object *target_obj;
  2665. struct drm_i915_gem_object *target_obj_priv;
  2666. uint32_t reloc_val, reloc_offset;
  2667. uint32_t __iomem *reloc_entry;
  2668. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  2669. reloc->target_handle);
  2670. if (target_obj == NULL) {
  2671. i915_gem_object_unpin(obj);
  2672. return -ENOENT;
  2673. }
  2674. target_obj_priv = to_intel_bo(target_obj);
  2675. #if WATCH_RELOC
  2676. DRM_INFO("%s: obj %p offset %08x target %d "
  2677. "read %08x write %08x gtt %08x "
  2678. "presumed %08x delta %08x\n",
  2679. __func__,
  2680. obj,
  2681. (int) reloc->offset,
  2682. (int) reloc->target_handle,
  2683. (int) reloc->read_domains,
  2684. (int) reloc->write_domain,
  2685. (int) target_obj_priv->gtt_offset,
  2686. (int) reloc->presumed_offset,
  2687. reloc->delta);
  2688. #endif
  2689. /* The target buffer should have appeared before us in the
  2690. * exec_object list, so it should have a GTT space bound by now.
  2691. */
  2692. if (target_obj_priv->gtt_space == NULL) {
  2693. DRM_ERROR("No GTT space found for object %d\n",
  2694. reloc->target_handle);
  2695. drm_gem_object_unreference(target_obj);
  2696. i915_gem_object_unpin(obj);
  2697. return -EINVAL;
  2698. }
  2699. /* Validate that the target is in a valid r/w GPU domain */
  2700. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2701. DRM_ERROR("reloc with multiple write domains: "
  2702. "obj %p target %d offset %d "
  2703. "read %08x write %08x",
  2704. obj, reloc->target_handle,
  2705. (int) reloc->offset,
  2706. reloc->read_domains,
  2707. reloc->write_domain);
  2708. return -EINVAL;
  2709. }
  2710. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2711. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2712. DRM_ERROR("reloc with read/write CPU domains: "
  2713. "obj %p target %d offset %d "
  2714. "read %08x write %08x",
  2715. obj, reloc->target_handle,
  2716. (int) reloc->offset,
  2717. reloc->read_domains,
  2718. reloc->write_domain);
  2719. drm_gem_object_unreference(target_obj);
  2720. i915_gem_object_unpin(obj);
  2721. return -EINVAL;
  2722. }
  2723. if (reloc->write_domain && target_obj->pending_write_domain &&
  2724. reloc->write_domain != target_obj->pending_write_domain) {
  2725. DRM_ERROR("Write domain conflict: "
  2726. "obj %p target %d offset %d "
  2727. "new %08x old %08x\n",
  2728. obj, reloc->target_handle,
  2729. (int) reloc->offset,
  2730. reloc->write_domain,
  2731. target_obj->pending_write_domain);
  2732. drm_gem_object_unreference(target_obj);
  2733. i915_gem_object_unpin(obj);
  2734. return -EINVAL;
  2735. }
  2736. target_obj->pending_read_domains |= reloc->read_domains;
  2737. target_obj->pending_write_domain |= reloc->write_domain;
  2738. /* If the relocation already has the right value in it, no
  2739. * more work needs to be done.
  2740. */
  2741. if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
  2742. drm_gem_object_unreference(target_obj);
  2743. continue;
  2744. }
  2745. /* Check that the relocation address is valid... */
  2746. if (reloc->offset > obj->size - 4) {
  2747. DRM_ERROR("Relocation beyond object bounds: "
  2748. "obj %p target %d offset %d size %d.\n",
  2749. obj, reloc->target_handle,
  2750. (int) reloc->offset, (int) obj->size);
  2751. drm_gem_object_unreference(target_obj);
  2752. i915_gem_object_unpin(obj);
  2753. return -EINVAL;
  2754. }
  2755. if (reloc->offset & 3) {
  2756. DRM_ERROR("Relocation not 4-byte aligned: "
  2757. "obj %p target %d offset %d.\n",
  2758. obj, reloc->target_handle,
  2759. (int) reloc->offset);
  2760. drm_gem_object_unreference(target_obj);
  2761. i915_gem_object_unpin(obj);
  2762. return -EINVAL;
  2763. }
  2764. /* and points to somewhere within the target object. */
  2765. if (reloc->delta >= target_obj->size) {
  2766. DRM_ERROR("Relocation beyond target object bounds: "
  2767. "obj %p target %d delta %d size %d.\n",
  2768. obj, reloc->target_handle,
  2769. (int) reloc->delta, (int) target_obj->size);
  2770. drm_gem_object_unreference(target_obj);
  2771. i915_gem_object_unpin(obj);
  2772. return -EINVAL;
  2773. }
  2774. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2775. if (ret != 0) {
  2776. drm_gem_object_unreference(target_obj);
  2777. i915_gem_object_unpin(obj);
  2778. return -EINVAL;
  2779. }
  2780. /* Map the page containing the relocation we're going to
  2781. * perform.
  2782. */
  2783. reloc_offset = obj_priv->gtt_offset + reloc->offset;
  2784. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2785. (reloc_offset &
  2786. ~(PAGE_SIZE - 1)),
  2787. KM_USER0);
  2788. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2789. (reloc_offset & (PAGE_SIZE - 1)));
  2790. reloc_val = target_obj_priv->gtt_offset + reloc->delta;
  2791. #if WATCH_BUF
  2792. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2793. obj, (unsigned int) reloc->offset,
  2794. readl(reloc_entry), reloc_val);
  2795. #endif
  2796. writel(reloc_val, reloc_entry);
  2797. io_mapping_unmap_atomic(reloc_page, KM_USER0);
  2798. /* The updated presumed offset for this entry will be
  2799. * copied back out to the user.
  2800. */
  2801. reloc->presumed_offset = target_obj_priv->gtt_offset;
  2802. drm_gem_object_unreference(target_obj);
  2803. }
  2804. #if WATCH_BUF
  2805. if (0)
  2806. i915_gem_dump_object(obj, 128, __func__, ~0);
  2807. #endif
  2808. return 0;
  2809. }
  2810. /* Throttle our rendering by waiting until the ring has completed our requests
  2811. * emitted over 20 msec ago.
  2812. *
  2813. * Note that if we were to use the current jiffies each time around the loop,
  2814. * we wouldn't escape the function with any frames outstanding if the time to
  2815. * render a frame was over 20ms.
  2816. *
  2817. * This should get us reasonable parallelism between CPU and GPU but also
  2818. * relatively low latency when blocking on a particular request to finish.
  2819. */
  2820. static int
  2821. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2822. {
  2823. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2824. int ret = 0;
  2825. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2826. mutex_lock(&dev->struct_mutex);
  2827. while (!list_empty(&i915_file_priv->mm.request_list)) {
  2828. struct drm_i915_gem_request *request;
  2829. request = list_first_entry(&i915_file_priv->mm.request_list,
  2830. struct drm_i915_gem_request,
  2831. client_list);
  2832. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2833. break;
  2834. ret = i915_wait_request(dev, request->seqno, request->ring);
  2835. if (ret != 0)
  2836. break;
  2837. }
  2838. mutex_unlock(&dev->struct_mutex);
  2839. return ret;
  2840. }
  2841. static int
  2842. i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
  2843. uint32_t buffer_count,
  2844. struct drm_i915_gem_relocation_entry **relocs)
  2845. {
  2846. uint32_t reloc_count = 0, reloc_index = 0, i;
  2847. int ret;
  2848. *relocs = NULL;
  2849. for (i = 0; i < buffer_count; i++) {
  2850. if (reloc_count + exec_list[i].relocation_count < reloc_count)
  2851. return -EINVAL;
  2852. reloc_count += exec_list[i].relocation_count;
  2853. }
  2854. *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
  2855. if (*relocs == NULL) {
  2856. DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
  2857. return -ENOMEM;
  2858. }
  2859. for (i = 0; i < buffer_count; i++) {
  2860. struct drm_i915_gem_relocation_entry __user *user_relocs;
  2861. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  2862. ret = copy_from_user(&(*relocs)[reloc_index],
  2863. user_relocs,
  2864. exec_list[i].relocation_count *
  2865. sizeof(**relocs));
  2866. if (ret != 0) {
  2867. drm_free_large(*relocs);
  2868. *relocs = NULL;
  2869. return -EFAULT;
  2870. }
  2871. reloc_index += exec_list[i].relocation_count;
  2872. }
  2873. return 0;
  2874. }
  2875. static int
  2876. i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
  2877. uint32_t buffer_count,
  2878. struct drm_i915_gem_relocation_entry *relocs)
  2879. {
  2880. uint32_t reloc_count = 0, i;
  2881. int ret = 0;
  2882. if (relocs == NULL)
  2883. return 0;
  2884. for (i = 0; i < buffer_count; i++) {
  2885. struct drm_i915_gem_relocation_entry __user *user_relocs;
  2886. int unwritten;
  2887. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  2888. unwritten = copy_to_user(user_relocs,
  2889. &relocs[reloc_count],
  2890. exec_list[i].relocation_count *
  2891. sizeof(*relocs));
  2892. if (unwritten) {
  2893. ret = -EFAULT;
  2894. goto err;
  2895. }
  2896. reloc_count += exec_list[i].relocation_count;
  2897. }
  2898. err:
  2899. drm_free_large(relocs);
  2900. return ret;
  2901. }
  2902. static int
  2903. i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
  2904. uint64_t exec_offset)
  2905. {
  2906. uint32_t exec_start, exec_len;
  2907. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2908. exec_len = (uint32_t) exec->batch_len;
  2909. if ((exec_start | exec_len) & 0x7)
  2910. return -EINVAL;
  2911. if (!exec_start)
  2912. return -EINVAL;
  2913. return 0;
  2914. }
  2915. static int
  2916. i915_gem_wait_for_pending_flip(struct drm_device *dev,
  2917. struct drm_gem_object **object_list,
  2918. int count)
  2919. {
  2920. drm_i915_private_t *dev_priv = dev->dev_private;
  2921. struct drm_i915_gem_object *obj_priv;
  2922. DEFINE_WAIT(wait);
  2923. int i, ret = 0;
  2924. for (;;) {
  2925. prepare_to_wait(&dev_priv->pending_flip_queue,
  2926. &wait, TASK_INTERRUPTIBLE);
  2927. for (i = 0; i < count; i++) {
  2928. obj_priv = to_intel_bo(object_list[i]);
  2929. if (atomic_read(&obj_priv->pending_flip) > 0)
  2930. break;
  2931. }
  2932. if (i == count)
  2933. break;
  2934. if (!signal_pending(current)) {
  2935. mutex_unlock(&dev->struct_mutex);
  2936. schedule();
  2937. mutex_lock(&dev->struct_mutex);
  2938. continue;
  2939. }
  2940. ret = -ERESTARTSYS;
  2941. break;
  2942. }
  2943. finish_wait(&dev_priv->pending_flip_queue, &wait);
  2944. return ret;
  2945. }
  2946. static int
  2947. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  2948. struct drm_file *file_priv,
  2949. struct drm_i915_gem_execbuffer2 *args,
  2950. struct drm_i915_gem_exec_object2 *exec_list)
  2951. {
  2952. drm_i915_private_t *dev_priv = dev->dev_private;
  2953. struct drm_gem_object **object_list = NULL;
  2954. struct drm_gem_object *batch_obj;
  2955. struct drm_i915_gem_object *obj_priv;
  2956. struct drm_clip_rect *cliprects = NULL;
  2957. struct drm_i915_gem_relocation_entry *relocs = NULL;
  2958. struct drm_i915_gem_request *request = NULL;
  2959. int ret = 0, ret2, i, pinned = 0;
  2960. uint64_t exec_offset;
  2961. uint32_t seqno, reloc_index;
  2962. int pin_tries, flips;
  2963. struct intel_ring_buffer *ring = NULL;
  2964. #if WATCH_EXEC
  2965. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  2966. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  2967. #endif
  2968. if (args->flags & I915_EXEC_BSD) {
  2969. if (!HAS_BSD(dev)) {
  2970. DRM_ERROR("execbuf with wrong flag\n");
  2971. return -EINVAL;
  2972. }
  2973. ring = &dev_priv->bsd_ring;
  2974. } else {
  2975. ring = &dev_priv->render_ring;
  2976. }
  2977. if (args->buffer_count < 1) {
  2978. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  2979. return -EINVAL;
  2980. }
  2981. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  2982. if (object_list == NULL) {
  2983. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  2984. args->buffer_count);
  2985. ret = -ENOMEM;
  2986. goto pre_mutex_err;
  2987. }
  2988. if (args->num_cliprects != 0) {
  2989. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  2990. GFP_KERNEL);
  2991. if (cliprects == NULL) {
  2992. ret = -ENOMEM;
  2993. goto pre_mutex_err;
  2994. }
  2995. ret = copy_from_user(cliprects,
  2996. (struct drm_clip_rect __user *)
  2997. (uintptr_t) args->cliprects_ptr,
  2998. sizeof(*cliprects) * args->num_cliprects);
  2999. if (ret != 0) {
  3000. DRM_ERROR("copy %d cliprects failed: %d\n",
  3001. args->num_cliprects, ret);
  3002. ret = -EFAULT;
  3003. goto pre_mutex_err;
  3004. }
  3005. }
  3006. request = kzalloc(sizeof(*request), GFP_KERNEL);
  3007. if (request == NULL) {
  3008. ret = -ENOMEM;
  3009. goto pre_mutex_err;
  3010. }
  3011. ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
  3012. &relocs);
  3013. if (ret != 0)
  3014. goto pre_mutex_err;
  3015. mutex_lock(&dev->struct_mutex);
  3016. i915_verify_inactive(dev, __FILE__, __LINE__);
  3017. if (atomic_read(&dev_priv->mm.wedged)) {
  3018. mutex_unlock(&dev->struct_mutex);
  3019. ret = -EIO;
  3020. goto pre_mutex_err;
  3021. }
  3022. if (dev_priv->mm.suspended) {
  3023. mutex_unlock(&dev->struct_mutex);
  3024. ret = -EBUSY;
  3025. goto pre_mutex_err;
  3026. }
  3027. /* Look up object handles */
  3028. flips = 0;
  3029. for (i = 0; i < args->buffer_count; i++) {
  3030. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  3031. exec_list[i].handle);
  3032. if (object_list[i] == NULL) {
  3033. DRM_ERROR("Invalid object handle %d at index %d\n",
  3034. exec_list[i].handle, i);
  3035. /* prevent error path from reading uninitialized data */
  3036. args->buffer_count = i + 1;
  3037. ret = -ENOENT;
  3038. goto err;
  3039. }
  3040. obj_priv = to_intel_bo(object_list[i]);
  3041. if (obj_priv->in_execbuffer) {
  3042. DRM_ERROR("Object %p appears more than once in object list\n",
  3043. object_list[i]);
  3044. /* prevent error path from reading uninitialized data */
  3045. args->buffer_count = i + 1;
  3046. ret = -EINVAL;
  3047. goto err;
  3048. }
  3049. obj_priv->in_execbuffer = true;
  3050. flips += atomic_read(&obj_priv->pending_flip);
  3051. }
  3052. if (flips > 0) {
  3053. ret = i915_gem_wait_for_pending_flip(dev, object_list,
  3054. args->buffer_count);
  3055. if (ret)
  3056. goto err;
  3057. }
  3058. /* Pin and relocate */
  3059. for (pin_tries = 0; ; pin_tries++) {
  3060. ret = 0;
  3061. reloc_index = 0;
  3062. for (i = 0; i < args->buffer_count; i++) {
  3063. object_list[i]->pending_read_domains = 0;
  3064. object_list[i]->pending_write_domain = 0;
  3065. ret = i915_gem_object_pin_and_relocate(object_list[i],
  3066. file_priv,
  3067. &exec_list[i],
  3068. &relocs[reloc_index]);
  3069. if (ret)
  3070. break;
  3071. pinned = i + 1;
  3072. reloc_index += exec_list[i].relocation_count;
  3073. }
  3074. /* success */
  3075. if (ret == 0)
  3076. break;
  3077. /* error other than GTT full, or we've already tried again */
  3078. if (ret != -ENOSPC || pin_tries >= 1) {
  3079. if (ret != -ERESTARTSYS) {
  3080. unsigned long long total_size = 0;
  3081. int num_fences = 0;
  3082. for (i = 0; i < args->buffer_count; i++) {
  3083. obj_priv = to_intel_bo(object_list[i]);
  3084. total_size += object_list[i]->size;
  3085. num_fences +=
  3086. exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
  3087. obj_priv->tiling_mode != I915_TILING_NONE;
  3088. }
  3089. DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
  3090. pinned+1, args->buffer_count,
  3091. total_size, num_fences,
  3092. ret);
  3093. DRM_ERROR("%d objects [%d pinned], "
  3094. "%d object bytes [%d pinned], "
  3095. "%d/%d gtt bytes\n",
  3096. atomic_read(&dev->object_count),
  3097. atomic_read(&dev->pin_count),
  3098. atomic_read(&dev->object_memory),
  3099. atomic_read(&dev->pin_memory),
  3100. atomic_read(&dev->gtt_memory),
  3101. dev->gtt_total);
  3102. }
  3103. goto err;
  3104. }
  3105. /* unpin all of our buffers */
  3106. for (i = 0; i < pinned; i++)
  3107. i915_gem_object_unpin(object_list[i]);
  3108. pinned = 0;
  3109. /* evict everyone we can from the aperture */
  3110. ret = i915_gem_evict_everything(dev);
  3111. if (ret && ret != -ENOSPC)
  3112. goto err;
  3113. }
  3114. /* Set the pending read domains for the batch buffer to COMMAND */
  3115. batch_obj = object_list[args->buffer_count-1];
  3116. if (batch_obj->pending_write_domain) {
  3117. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3118. ret = -EINVAL;
  3119. goto err;
  3120. }
  3121. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3122. /* Sanity check the batch buffer, prior to moving objects */
  3123. exec_offset = exec_list[args->buffer_count - 1].offset;
  3124. ret = i915_gem_check_execbuffer (args, exec_offset);
  3125. if (ret != 0) {
  3126. DRM_ERROR("execbuf with invalid offset/length\n");
  3127. goto err;
  3128. }
  3129. i915_verify_inactive(dev, __FILE__, __LINE__);
  3130. /* Zero the global flush/invalidate flags. These
  3131. * will be modified as new domains are computed
  3132. * for each object
  3133. */
  3134. dev->invalidate_domains = 0;
  3135. dev->flush_domains = 0;
  3136. for (i = 0; i < args->buffer_count; i++) {
  3137. struct drm_gem_object *obj = object_list[i];
  3138. /* Compute new gpu domains and update invalidate/flush */
  3139. i915_gem_object_set_to_gpu_domain(obj);
  3140. }
  3141. i915_verify_inactive(dev, __FILE__, __LINE__);
  3142. if (dev->invalidate_domains | dev->flush_domains) {
  3143. #if WATCH_EXEC
  3144. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3145. __func__,
  3146. dev->invalidate_domains,
  3147. dev->flush_domains);
  3148. #endif
  3149. i915_gem_flush(dev,
  3150. dev->invalidate_domains,
  3151. dev->flush_domains);
  3152. }
  3153. if (dev_priv->render_ring.outstanding_lazy_request) {
  3154. (void)i915_add_request(dev, file_priv, NULL, &dev_priv->render_ring);
  3155. dev_priv->render_ring.outstanding_lazy_request = false;
  3156. }
  3157. if (dev_priv->bsd_ring.outstanding_lazy_request) {
  3158. (void)i915_add_request(dev, file_priv, NULL, &dev_priv->bsd_ring);
  3159. dev_priv->bsd_ring.outstanding_lazy_request = false;
  3160. }
  3161. for (i = 0; i < args->buffer_count; i++) {
  3162. struct drm_gem_object *obj = object_list[i];
  3163. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3164. uint32_t old_write_domain = obj->write_domain;
  3165. obj->write_domain = obj->pending_write_domain;
  3166. if (obj->write_domain)
  3167. list_move_tail(&obj_priv->gpu_write_list,
  3168. &dev_priv->mm.gpu_write_list);
  3169. else
  3170. list_del_init(&obj_priv->gpu_write_list);
  3171. trace_i915_gem_object_change_domain(obj,
  3172. obj->read_domains,
  3173. old_write_domain);
  3174. }
  3175. i915_verify_inactive(dev, __FILE__, __LINE__);
  3176. #if WATCH_COHERENCY
  3177. for (i = 0; i < args->buffer_count; i++) {
  3178. i915_gem_object_check_coherency(object_list[i],
  3179. exec_list[i].handle);
  3180. }
  3181. #endif
  3182. #if WATCH_EXEC
  3183. i915_gem_dump_object(batch_obj,
  3184. args->batch_len,
  3185. __func__,
  3186. ~0);
  3187. #endif
  3188. /* Exec the batchbuffer */
  3189. ret = ring->dispatch_gem_execbuffer(dev, ring, args,
  3190. cliprects, exec_offset);
  3191. if (ret) {
  3192. DRM_ERROR("dispatch failed %d\n", ret);
  3193. goto err;
  3194. }
  3195. /*
  3196. * Ensure that the commands in the batch buffer are
  3197. * finished before the interrupt fires
  3198. */
  3199. i915_retire_commands(dev, ring);
  3200. i915_verify_inactive(dev, __FILE__, __LINE__);
  3201. for (i = 0; i < args->buffer_count; i++) {
  3202. struct drm_gem_object *obj = object_list[i];
  3203. obj_priv = to_intel_bo(obj);
  3204. i915_gem_object_move_to_active(obj, ring);
  3205. #if WATCH_LRU
  3206. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  3207. #endif
  3208. }
  3209. /*
  3210. * Get a seqno representing the execution of the current buffer,
  3211. * which we can wait on. We would like to mitigate these interrupts,
  3212. * likely by only creating seqnos occasionally (so that we have
  3213. * *some* interrupts representing completion of buffers that we can
  3214. * wait on when trying to clear up gtt space).
  3215. */
  3216. seqno = i915_add_request(dev, file_priv, request, ring);
  3217. request = NULL;
  3218. #if WATCH_LRU
  3219. i915_dump_lru(dev, __func__);
  3220. #endif
  3221. i915_verify_inactive(dev, __FILE__, __LINE__);
  3222. err:
  3223. for (i = 0; i < pinned; i++)
  3224. i915_gem_object_unpin(object_list[i]);
  3225. for (i = 0; i < args->buffer_count; i++) {
  3226. if (object_list[i]) {
  3227. obj_priv = to_intel_bo(object_list[i]);
  3228. obj_priv->in_execbuffer = false;
  3229. }
  3230. drm_gem_object_unreference(object_list[i]);
  3231. }
  3232. mutex_unlock(&dev->struct_mutex);
  3233. pre_mutex_err:
  3234. /* Copy the updated relocations out regardless of current error
  3235. * state. Failure to update the relocs would mean that the next
  3236. * time userland calls execbuf, it would do so with presumed offset
  3237. * state that didn't match the actual object state.
  3238. */
  3239. ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
  3240. relocs);
  3241. if (ret2 != 0) {
  3242. DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
  3243. if (ret == 0)
  3244. ret = ret2;
  3245. }
  3246. drm_free_large(object_list);
  3247. kfree(cliprects);
  3248. kfree(request);
  3249. return ret;
  3250. }
  3251. /*
  3252. * Legacy execbuffer just creates an exec2 list from the original exec object
  3253. * list array and passes it to the real function.
  3254. */
  3255. int
  3256. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3257. struct drm_file *file_priv)
  3258. {
  3259. struct drm_i915_gem_execbuffer *args = data;
  3260. struct drm_i915_gem_execbuffer2 exec2;
  3261. struct drm_i915_gem_exec_object *exec_list = NULL;
  3262. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3263. int ret, i;
  3264. #if WATCH_EXEC
  3265. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3266. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3267. #endif
  3268. if (args->buffer_count < 1) {
  3269. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3270. return -EINVAL;
  3271. }
  3272. /* Copy in the exec list from userland */
  3273. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3274. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3275. if (exec_list == NULL || exec2_list == NULL) {
  3276. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3277. args->buffer_count);
  3278. drm_free_large(exec_list);
  3279. drm_free_large(exec2_list);
  3280. return -ENOMEM;
  3281. }
  3282. ret = copy_from_user(exec_list,
  3283. (struct drm_i915_relocation_entry __user *)
  3284. (uintptr_t) args->buffers_ptr,
  3285. sizeof(*exec_list) * args->buffer_count);
  3286. if (ret != 0) {
  3287. DRM_ERROR("copy %d exec entries failed %d\n",
  3288. args->buffer_count, ret);
  3289. drm_free_large(exec_list);
  3290. drm_free_large(exec2_list);
  3291. return -EFAULT;
  3292. }
  3293. for (i = 0; i < args->buffer_count; i++) {
  3294. exec2_list[i].handle = exec_list[i].handle;
  3295. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3296. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3297. exec2_list[i].alignment = exec_list[i].alignment;
  3298. exec2_list[i].offset = exec_list[i].offset;
  3299. if (!IS_I965G(dev))
  3300. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3301. else
  3302. exec2_list[i].flags = 0;
  3303. }
  3304. exec2.buffers_ptr = args->buffers_ptr;
  3305. exec2.buffer_count = args->buffer_count;
  3306. exec2.batch_start_offset = args->batch_start_offset;
  3307. exec2.batch_len = args->batch_len;
  3308. exec2.DR1 = args->DR1;
  3309. exec2.DR4 = args->DR4;
  3310. exec2.num_cliprects = args->num_cliprects;
  3311. exec2.cliprects_ptr = args->cliprects_ptr;
  3312. exec2.flags = I915_EXEC_RENDER;
  3313. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3314. if (!ret) {
  3315. /* Copy the new buffer offsets back to the user's exec list. */
  3316. for (i = 0; i < args->buffer_count; i++)
  3317. exec_list[i].offset = exec2_list[i].offset;
  3318. /* ... and back out to userspace */
  3319. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3320. (uintptr_t) args->buffers_ptr,
  3321. exec_list,
  3322. sizeof(*exec_list) * args->buffer_count);
  3323. if (ret) {
  3324. ret = -EFAULT;
  3325. DRM_ERROR("failed to copy %d exec entries "
  3326. "back to user (%d)\n",
  3327. args->buffer_count, ret);
  3328. }
  3329. }
  3330. drm_free_large(exec_list);
  3331. drm_free_large(exec2_list);
  3332. return ret;
  3333. }
  3334. int
  3335. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3336. struct drm_file *file_priv)
  3337. {
  3338. struct drm_i915_gem_execbuffer2 *args = data;
  3339. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3340. int ret;
  3341. #if WATCH_EXEC
  3342. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3343. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3344. #endif
  3345. if (args->buffer_count < 1) {
  3346. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3347. return -EINVAL;
  3348. }
  3349. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3350. if (exec2_list == NULL) {
  3351. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3352. args->buffer_count);
  3353. return -ENOMEM;
  3354. }
  3355. ret = copy_from_user(exec2_list,
  3356. (struct drm_i915_relocation_entry __user *)
  3357. (uintptr_t) args->buffers_ptr,
  3358. sizeof(*exec2_list) * args->buffer_count);
  3359. if (ret != 0) {
  3360. DRM_ERROR("copy %d exec entries failed %d\n",
  3361. args->buffer_count, ret);
  3362. drm_free_large(exec2_list);
  3363. return -EFAULT;
  3364. }
  3365. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3366. if (!ret) {
  3367. /* Copy the new buffer offsets back to the user's exec list. */
  3368. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3369. (uintptr_t) args->buffers_ptr,
  3370. exec2_list,
  3371. sizeof(*exec2_list) * args->buffer_count);
  3372. if (ret) {
  3373. ret = -EFAULT;
  3374. DRM_ERROR("failed to copy %d exec entries "
  3375. "back to user (%d)\n",
  3376. args->buffer_count, ret);
  3377. }
  3378. }
  3379. drm_free_large(exec2_list);
  3380. return ret;
  3381. }
  3382. int
  3383. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  3384. {
  3385. struct drm_device *dev = obj->dev;
  3386. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3387. int ret;
  3388. BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  3389. i915_verify_inactive(dev, __FILE__, __LINE__);
  3390. if (obj_priv->gtt_space != NULL) {
  3391. if (alignment == 0)
  3392. alignment = i915_gem_get_gtt_alignment(obj);
  3393. if (obj_priv->gtt_offset & (alignment - 1)) {
  3394. WARN(obj_priv->pin_count,
  3395. "bo is already pinned with incorrect alignment:"
  3396. " offset=%x, req.alignment=%x\n",
  3397. obj_priv->gtt_offset, alignment);
  3398. ret = i915_gem_object_unbind(obj);
  3399. if (ret)
  3400. return ret;
  3401. }
  3402. }
  3403. if (obj_priv->gtt_space == NULL) {
  3404. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  3405. if (ret)
  3406. return ret;
  3407. }
  3408. obj_priv->pin_count++;
  3409. /* If the object is not active and not pending a flush,
  3410. * remove it from the inactive list
  3411. */
  3412. if (obj_priv->pin_count == 1) {
  3413. atomic_inc(&dev->pin_count);
  3414. atomic_add(obj->size, &dev->pin_memory);
  3415. if (!obj_priv->active &&
  3416. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3417. list_del_init(&obj_priv->list);
  3418. }
  3419. i915_verify_inactive(dev, __FILE__, __LINE__);
  3420. return 0;
  3421. }
  3422. void
  3423. i915_gem_object_unpin(struct drm_gem_object *obj)
  3424. {
  3425. struct drm_device *dev = obj->dev;
  3426. drm_i915_private_t *dev_priv = dev->dev_private;
  3427. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3428. i915_verify_inactive(dev, __FILE__, __LINE__);
  3429. obj_priv->pin_count--;
  3430. BUG_ON(obj_priv->pin_count < 0);
  3431. BUG_ON(obj_priv->gtt_space == NULL);
  3432. /* If the object is no longer pinned, and is
  3433. * neither active nor being flushed, then stick it on
  3434. * the inactive list
  3435. */
  3436. if (obj_priv->pin_count == 0) {
  3437. if (!obj_priv->active &&
  3438. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3439. list_move_tail(&obj_priv->list,
  3440. &dev_priv->mm.inactive_list);
  3441. atomic_dec(&dev->pin_count);
  3442. atomic_sub(obj->size, &dev->pin_memory);
  3443. }
  3444. i915_verify_inactive(dev, __FILE__, __LINE__);
  3445. }
  3446. int
  3447. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3448. struct drm_file *file_priv)
  3449. {
  3450. struct drm_i915_gem_pin *args = data;
  3451. struct drm_gem_object *obj;
  3452. struct drm_i915_gem_object *obj_priv;
  3453. int ret;
  3454. mutex_lock(&dev->struct_mutex);
  3455. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3456. if (obj == NULL) {
  3457. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  3458. args->handle);
  3459. mutex_unlock(&dev->struct_mutex);
  3460. return -ENOENT;
  3461. }
  3462. obj_priv = to_intel_bo(obj);
  3463. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3464. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3465. drm_gem_object_unreference(obj);
  3466. mutex_unlock(&dev->struct_mutex);
  3467. return -EINVAL;
  3468. }
  3469. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3470. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3471. args->handle);
  3472. drm_gem_object_unreference(obj);
  3473. mutex_unlock(&dev->struct_mutex);
  3474. return -EINVAL;
  3475. }
  3476. obj_priv->user_pin_count++;
  3477. obj_priv->pin_filp = file_priv;
  3478. if (obj_priv->user_pin_count == 1) {
  3479. ret = i915_gem_object_pin(obj, args->alignment);
  3480. if (ret != 0) {
  3481. drm_gem_object_unreference(obj);
  3482. mutex_unlock(&dev->struct_mutex);
  3483. return ret;
  3484. }
  3485. }
  3486. /* XXX - flush the CPU caches for pinned objects
  3487. * as the X server doesn't manage domains yet
  3488. */
  3489. i915_gem_object_flush_cpu_write_domain(obj);
  3490. args->offset = obj_priv->gtt_offset;
  3491. drm_gem_object_unreference(obj);
  3492. mutex_unlock(&dev->struct_mutex);
  3493. return 0;
  3494. }
  3495. int
  3496. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3497. struct drm_file *file_priv)
  3498. {
  3499. struct drm_i915_gem_pin *args = data;
  3500. struct drm_gem_object *obj;
  3501. struct drm_i915_gem_object *obj_priv;
  3502. mutex_lock(&dev->struct_mutex);
  3503. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3504. if (obj == NULL) {
  3505. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  3506. args->handle);
  3507. mutex_unlock(&dev->struct_mutex);
  3508. return -ENOENT;
  3509. }
  3510. obj_priv = to_intel_bo(obj);
  3511. if (obj_priv->pin_filp != file_priv) {
  3512. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3513. args->handle);
  3514. drm_gem_object_unreference(obj);
  3515. mutex_unlock(&dev->struct_mutex);
  3516. return -EINVAL;
  3517. }
  3518. obj_priv->user_pin_count--;
  3519. if (obj_priv->user_pin_count == 0) {
  3520. obj_priv->pin_filp = NULL;
  3521. i915_gem_object_unpin(obj);
  3522. }
  3523. drm_gem_object_unreference(obj);
  3524. mutex_unlock(&dev->struct_mutex);
  3525. return 0;
  3526. }
  3527. int
  3528. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3529. struct drm_file *file_priv)
  3530. {
  3531. struct drm_i915_gem_busy *args = data;
  3532. struct drm_gem_object *obj;
  3533. struct drm_i915_gem_object *obj_priv;
  3534. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3535. if (obj == NULL) {
  3536. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  3537. args->handle);
  3538. return -ENOENT;
  3539. }
  3540. mutex_lock(&dev->struct_mutex);
  3541. /* Count all active objects as busy, even if they are currently not used
  3542. * by the gpu. Users of this interface expect objects to eventually
  3543. * become non-busy without any further actions, therefore emit any
  3544. * necessary flushes here.
  3545. */
  3546. obj_priv = to_intel_bo(obj);
  3547. args->busy = obj_priv->active;
  3548. if (args->busy) {
  3549. /* Unconditionally flush objects, even when the gpu still uses this
  3550. * object. Userspace calling this function indicates that it wants to
  3551. * use this buffer rather sooner than later, so issuing the required
  3552. * flush earlier is beneficial.
  3553. */
  3554. if (obj->write_domain) {
  3555. i915_gem_flush(dev, 0, obj->write_domain);
  3556. (void)i915_add_request(dev, file_priv, NULL, obj_priv->ring);
  3557. }
  3558. /* Update the active list for the hardware's current position.
  3559. * Otherwise this only updates on a delayed timer or when irqs
  3560. * are actually unmasked, and our working set ends up being
  3561. * larger than required.
  3562. */
  3563. i915_gem_retire_requests_ring(dev, obj_priv->ring);
  3564. args->busy = obj_priv->active;
  3565. }
  3566. drm_gem_object_unreference(obj);
  3567. mutex_unlock(&dev->struct_mutex);
  3568. return 0;
  3569. }
  3570. int
  3571. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3572. struct drm_file *file_priv)
  3573. {
  3574. return i915_gem_ring_throttle(dev, file_priv);
  3575. }
  3576. int
  3577. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3578. struct drm_file *file_priv)
  3579. {
  3580. struct drm_i915_gem_madvise *args = data;
  3581. struct drm_gem_object *obj;
  3582. struct drm_i915_gem_object *obj_priv;
  3583. switch (args->madv) {
  3584. case I915_MADV_DONTNEED:
  3585. case I915_MADV_WILLNEED:
  3586. break;
  3587. default:
  3588. return -EINVAL;
  3589. }
  3590. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3591. if (obj == NULL) {
  3592. DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
  3593. args->handle);
  3594. return -ENOENT;
  3595. }
  3596. mutex_lock(&dev->struct_mutex);
  3597. obj_priv = to_intel_bo(obj);
  3598. if (obj_priv->pin_count) {
  3599. drm_gem_object_unreference(obj);
  3600. mutex_unlock(&dev->struct_mutex);
  3601. DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
  3602. return -EINVAL;
  3603. }
  3604. if (obj_priv->madv != __I915_MADV_PURGED)
  3605. obj_priv->madv = args->madv;
  3606. /* if the object is no longer bound, discard its backing storage */
  3607. if (i915_gem_object_is_purgeable(obj_priv) &&
  3608. obj_priv->gtt_space == NULL)
  3609. i915_gem_object_truncate(obj);
  3610. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3611. drm_gem_object_unreference(obj);
  3612. mutex_unlock(&dev->struct_mutex);
  3613. return 0;
  3614. }
  3615. struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
  3616. size_t size)
  3617. {
  3618. struct drm_i915_gem_object *obj;
  3619. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3620. if (obj == NULL)
  3621. return NULL;
  3622. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3623. kfree(obj);
  3624. return NULL;
  3625. }
  3626. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3627. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3628. obj->agp_type = AGP_USER_MEMORY;
  3629. obj->base.driver_private = NULL;
  3630. obj->fence_reg = I915_FENCE_REG_NONE;
  3631. INIT_LIST_HEAD(&obj->list);
  3632. INIT_LIST_HEAD(&obj->gpu_write_list);
  3633. obj->madv = I915_MADV_WILLNEED;
  3634. trace_i915_gem_object_create(&obj->base);
  3635. return &obj->base;
  3636. }
  3637. int i915_gem_init_object(struct drm_gem_object *obj)
  3638. {
  3639. BUG();
  3640. return 0;
  3641. }
  3642. static void i915_gem_free_object_tail(struct drm_gem_object *obj)
  3643. {
  3644. struct drm_device *dev = obj->dev;
  3645. drm_i915_private_t *dev_priv = dev->dev_private;
  3646. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3647. int ret;
  3648. ret = i915_gem_object_unbind(obj);
  3649. if (ret == -ERESTARTSYS) {
  3650. list_move(&obj_priv->list,
  3651. &dev_priv->mm.deferred_free_list);
  3652. return;
  3653. }
  3654. if (obj_priv->mmap_offset)
  3655. i915_gem_free_mmap_offset(obj);
  3656. drm_gem_object_release(obj);
  3657. kfree(obj_priv->page_cpu_valid);
  3658. kfree(obj_priv->bit_17);
  3659. kfree(obj_priv);
  3660. }
  3661. void i915_gem_free_object(struct drm_gem_object *obj)
  3662. {
  3663. struct drm_device *dev = obj->dev;
  3664. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3665. trace_i915_gem_object_destroy(obj);
  3666. while (obj_priv->pin_count > 0)
  3667. i915_gem_object_unpin(obj);
  3668. if (obj_priv->phys_obj)
  3669. i915_gem_detach_phys_object(dev, obj);
  3670. i915_gem_free_object_tail(obj);
  3671. }
  3672. int
  3673. i915_gem_idle(struct drm_device *dev)
  3674. {
  3675. drm_i915_private_t *dev_priv = dev->dev_private;
  3676. int ret;
  3677. mutex_lock(&dev->struct_mutex);
  3678. if (dev_priv->mm.suspended ||
  3679. (dev_priv->render_ring.gem_object == NULL) ||
  3680. (HAS_BSD(dev) &&
  3681. dev_priv->bsd_ring.gem_object == NULL)) {
  3682. mutex_unlock(&dev->struct_mutex);
  3683. return 0;
  3684. }
  3685. ret = i915_gpu_idle(dev);
  3686. if (ret) {
  3687. mutex_unlock(&dev->struct_mutex);
  3688. return ret;
  3689. }
  3690. /* Under UMS, be paranoid and evict. */
  3691. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3692. ret = i915_gem_evict_inactive(dev);
  3693. if (ret) {
  3694. mutex_unlock(&dev->struct_mutex);
  3695. return ret;
  3696. }
  3697. }
  3698. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3699. * We need to replace this with a semaphore, or something.
  3700. * And not confound mm.suspended!
  3701. */
  3702. dev_priv->mm.suspended = 1;
  3703. del_timer_sync(&dev_priv->hangcheck_timer);
  3704. i915_kernel_lost_context(dev);
  3705. i915_gem_cleanup_ringbuffer(dev);
  3706. mutex_unlock(&dev->struct_mutex);
  3707. /* Cancel the retire work handler, which should be idle now. */
  3708. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3709. return 0;
  3710. }
  3711. /*
  3712. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  3713. * over cache flushing.
  3714. */
  3715. static int
  3716. i915_gem_init_pipe_control(struct drm_device *dev)
  3717. {
  3718. drm_i915_private_t *dev_priv = dev->dev_private;
  3719. struct drm_gem_object *obj;
  3720. struct drm_i915_gem_object *obj_priv;
  3721. int ret;
  3722. obj = i915_gem_alloc_object(dev, 4096);
  3723. if (obj == NULL) {
  3724. DRM_ERROR("Failed to allocate seqno page\n");
  3725. ret = -ENOMEM;
  3726. goto err;
  3727. }
  3728. obj_priv = to_intel_bo(obj);
  3729. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  3730. ret = i915_gem_object_pin(obj, 4096);
  3731. if (ret)
  3732. goto err_unref;
  3733. dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
  3734. dev_priv->seqno_page = kmap(obj_priv->pages[0]);
  3735. if (dev_priv->seqno_page == NULL)
  3736. goto err_unpin;
  3737. dev_priv->seqno_obj = obj;
  3738. memset(dev_priv->seqno_page, 0, PAGE_SIZE);
  3739. return 0;
  3740. err_unpin:
  3741. i915_gem_object_unpin(obj);
  3742. err_unref:
  3743. drm_gem_object_unreference(obj);
  3744. err:
  3745. return ret;
  3746. }
  3747. static void
  3748. i915_gem_cleanup_pipe_control(struct drm_device *dev)
  3749. {
  3750. drm_i915_private_t *dev_priv = dev->dev_private;
  3751. struct drm_gem_object *obj;
  3752. struct drm_i915_gem_object *obj_priv;
  3753. obj = dev_priv->seqno_obj;
  3754. obj_priv = to_intel_bo(obj);
  3755. kunmap(obj_priv->pages[0]);
  3756. i915_gem_object_unpin(obj);
  3757. drm_gem_object_unreference(obj);
  3758. dev_priv->seqno_obj = NULL;
  3759. dev_priv->seqno_page = NULL;
  3760. }
  3761. int
  3762. i915_gem_init_ringbuffer(struct drm_device *dev)
  3763. {
  3764. drm_i915_private_t *dev_priv = dev->dev_private;
  3765. int ret;
  3766. dev_priv->render_ring = render_ring;
  3767. if (!I915_NEED_GFX_HWS(dev)) {
  3768. dev_priv->render_ring.status_page.page_addr
  3769. = dev_priv->status_page_dmah->vaddr;
  3770. memset(dev_priv->render_ring.status_page.page_addr,
  3771. 0, PAGE_SIZE);
  3772. }
  3773. if (HAS_PIPE_CONTROL(dev)) {
  3774. ret = i915_gem_init_pipe_control(dev);
  3775. if (ret)
  3776. return ret;
  3777. }
  3778. ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
  3779. if (ret)
  3780. goto cleanup_pipe_control;
  3781. if (HAS_BSD(dev)) {
  3782. dev_priv->bsd_ring = bsd_ring;
  3783. ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
  3784. if (ret)
  3785. goto cleanup_render_ring;
  3786. }
  3787. dev_priv->next_seqno = 1;
  3788. return 0;
  3789. cleanup_render_ring:
  3790. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3791. cleanup_pipe_control:
  3792. if (HAS_PIPE_CONTROL(dev))
  3793. i915_gem_cleanup_pipe_control(dev);
  3794. return ret;
  3795. }
  3796. void
  3797. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3798. {
  3799. drm_i915_private_t *dev_priv = dev->dev_private;
  3800. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3801. if (HAS_BSD(dev))
  3802. intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
  3803. if (HAS_PIPE_CONTROL(dev))
  3804. i915_gem_cleanup_pipe_control(dev);
  3805. }
  3806. int
  3807. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3808. struct drm_file *file_priv)
  3809. {
  3810. drm_i915_private_t *dev_priv = dev->dev_private;
  3811. int ret;
  3812. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3813. return 0;
  3814. if (atomic_read(&dev_priv->mm.wedged)) {
  3815. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3816. atomic_set(&dev_priv->mm.wedged, 0);
  3817. }
  3818. mutex_lock(&dev->struct_mutex);
  3819. dev_priv->mm.suspended = 0;
  3820. ret = i915_gem_init_ringbuffer(dev);
  3821. if (ret != 0) {
  3822. mutex_unlock(&dev->struct_mutex);
  3823. return ret;
  3824. }
  3825. BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
  3826. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
  3827. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3828. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3829. BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
  3830. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
  3831. mutex_unlock(&dev->struct_mutex);
  3832. ret = drm_irq_install(dev);
  3833. if (ret)
  3834. goto cleanup_ringbuffer;
  3835. return 0;
  3836. cleanup_ringbuffer:
  3837. mutex_lock(&dev->struct_mutex);
  3838. i915_gem_cleanup_ringbuffer(dev);
  3839. dev_priv->mm.suspended = 1;
  3840. mutex_unlock(&dev->struct_mutex);
  3841. return ret;
  3842. }
  3843. int
  3844. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3845. struct drm_file *file_priv)
  3846. {
  3847. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3848. return 0;
  3849. drm_irq_uninstall(dev);
  3850. return i915_gem_idle(dev);
  3851. }
  3852. void
  3853. i915_gem_lastclose(struct drm_device *dev)
  3854. {
  3855. int ret;
  3856. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3857. return;
  3858. ret = i915_gem_idle(dev);
  3859. if (ret)
  3860. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3861. }
  3862. void
  3863. i915_gem_load(struct drm_device *dev)
  3864. {
  3865. int i;
  3866. drm_i915_private_t *dev_priv = dev->dev_private;
  3867. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3868. INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
  3869. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3870. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3871. INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
  3872. INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
  3873. INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
  3874. if (HAS_BSD(dev)) {
  3875. INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
  3876. INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
  3877. }
  3878. for (i = 0; i < 16; i++)
  3879. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3880. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3881. i915_gem_retire_work_handler);
  3882. spin_lock(&shrink_list_lock);
  3883. list_add(&dev_priv->mm.shrink_list, &shrink_list);
  3884. spin_unlock(&shrink_list_lock);
  3885. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3886. if (IS_GEN3(dev)) {
  3887. u32 tmp = I915_READ(MI_ARB_STATE);
  3888. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  3889. /* arb state is a masked write, so set bit + bit in mask */
  3890. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  3891. I915_WRITE(MI_ARB_STATE, tmp);
  3892. }
  3893. }
  3894. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3895. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3896. dev_priv->fence_reg_start = 3;
  3897. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3898. dev_priv->num_fence_regs = 16;
  3899. else
  3900. dev_priv->num_fence_regs = 8;
  3901. /* Initialize fence registers to zero */
  3902. if (IS_I965G(dev)) {
  3903. for (i = 0; i < 16; i++)
  3904. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  3905. } else {
  3906. for (i = 0; i < 8; i++)
  3907. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  3908. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3909. for (i = 0; i < 8; i++)
  3910. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  3911. }
  3912. i915_gem_detect_bit_6_swizzle(dev);
  3913. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3914. }
  3915. /*
  3916. * Create a physically contiguous memory object for this object
  3917. * e.g. for cursor + overlay regs
  3918. */
  3919. static int i915_gem_init_phys_object(struct drm_device *dev,
  3920. int id, int size, int align)
  3921. {
  3922. drm_i915_private_t *dev_priv = dev->dev_private;
  3923. struct drm_i915_gem_phys_object *phys_obj;
  3924. int ret;
  3925. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3926. return 0;
  3927. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3928. if (!phys_obj)
  3929. return -ENOMEM;
  3930. phys_obj->id = id;
  3931. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3932. if (!phys_obj->handle) {
  3933. ret = -ENOMEM;
  3934. goto kfree_obj;
  3935. }
  3936. #ifdef CONFIG_X86
  3937. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3938. #endif
  3939. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3940. return 0;
  3941. kfree_obj:
  3942. kfree(phys_obj);
  3943. return ret;
  3944. }
  3945. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3946. {
  3947. drm_i915_private_t *dev_priv = dev->dev_private;
  3948. struct drm_i915_gem_phys_object *phys_obj;
  3949. if (!dev_priv->mm.phys_objs[id - 1])
  3950. return;
  3951. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3952. if (phys_obj->cur_obj) {
  3953. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3954. }
  3955. #ifdef CONFIG_X86
  3956. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3957. #endif
  3958. drm_pci_free(dev, phys_obj->handle);
  3959. kfree(phys_obj);
  3960. dev_priv->mm.phys_objs[id - 1] = NULL;
  3961. }
  3962. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3963. {
  3964. int i;
  3965. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3966. i915_gem_free_phys_object(dev, i);
  3967. }
  3968. void i915_gem_detach_phys_object(struct drm_device *dev,
  3969. struct drm_gem_object *obj)
  3970. {
  3971. struct drm_i915_gem_object *obj_priv;
  3972. int i;
  3973. int ret;
  3974. int page_count;
  3975. obj_priv = to_intel_bo(obj);
  3976. if (!obj_priv->phys_obj)
  3977. return;
  3978. ret = i915_gem_object_get_pages(obj, 0);
  3979. if (ret)
  3980. goto out;
  3981. page_count = obj->size / PAGE_SIZE;
  3982. for (i = 0; i < page_count; i++) {
  3983. char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
  3984. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3985. memcpy(dst, src, PAGE_SIZE);
  3986. kunmap_atomic(dst, KM_USER0);
  3987. }
  3988. drm_clflush_pages(obj_priv->pages, page_count);
  3989. drm_agp_chipset_flush(dev);
  3990. i915_gem_object_put_pages(obj);
  3991. out:
  3992. obj_priv->phys_obj->cur_obj = NULL;
  3993. obj_priv->phys_obj = NULL;
  3994. }
  3995. int
  3996. i915_gem_attach_phys_object(struct drm_device *dev,
  3997. struct drm_gem_object *obj,
  3998. int id,
  3999. int align)
  4000. {
  4001. drm_i915_private_t *dev_priv = dev->dev_private;
  4002. struct drm_i915_gem_object *obj_priv;
  4003. int ret = 0;
  4004. int page_count;
  4005. int i;
  4006. if (id > I915_MAX_PHYS_OBJECT)
  4007. return -EINVAL;
  4008. obj_priv = to_intel_bo(obj);
  4009. if (obj_priv->phys_obj) {
  4010. if (obj_priv->phys_obj->id == id)
  4011. return 0;
  4012. i915_gem_detach_phys_object(dev, obj);
  4013. }
  4014. /* create a new object */
  4015. if (!dev_priv->mm.phys_objs[id - 1]) {
  4016. ret = i915_gem_init_phys_object(dev, id,
  4017. obj->size, align);
  4018. if (ret) {
  4019. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4020. goto out;
  4021. }
  4022. }
  4023. /* bind to the object */
  4024. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4025. obj_priv->phys_obj->cur_obj = obj;
  4026. ret = i915_gem_object_get_pages(obj, 0);
  4027. if (ret) {
  4028. DRM_ERROR("failed to get page list\n");
  4029. goto out;
  4030. }
  4031. page_count = obj->size / PAGE_SIZE;
  4032. for (i = 0; i < page_count; i++) {
  4033. char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4034. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4035. memcpy(dst, src, PAGE_SIZE);
  4036. kunmap_atomic(src, KM_USER0);
  4037. }
  4038. i915_gem_object_put_pages(obj);
  4039. return 0;
  4040. out:
  4041. return ret;
  4042. }
  4043. static int
  4044. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4045. struct drm_i915_gem_pwrite *args,
  4046. struct drm_file *file_priv)
  4047. {
  4048. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4049. void *obj_addr;
  4050. int ret;
  4051. char __user *user_data;
  4052. user_data = (char __user *) (uintptr_t) args->data_ptr;
  4053. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4054. DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
  4055. ret = copy_from_user(obj_addr, user_data, args->size);
  4056. if (ret)
  4057. return -EFAULT;
  4058. drm_agp_chipset_flush(dev);
  4059. return 0;
  4060. }
  4061. void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
  4062. {
  4063. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  4064. /* Clean up our request list when the client is going away, so that
  4065. * later retire_requests won't dereference our soon-to-be-gone
  4066. * file_priv.
  4067. */
  4068. mutex_lock(&dev->struct_mutex);
  4069. while (!list_empty(&i915_file_priv->mm.request_list))
  4070. list_del_init(i915_file_priv->mm.request_list.next);
  4071. mutex_unlock(&dev->struct_mutex);
  4072. }
  4073. static int
  4074. i915_gpu_is_active(struct drm_device *dev)
  4075. {
  4076. drm_i915_private_t *dev_priv = dev->dev_private;
  4077. int lists_empty;
  4078. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  4079. list_empty(&dev_priv->render_ring.active_list);
  4080. if (HAS_BSD(dev))
  4081. lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
  4082. return !lists_empty;
  4083. }
  4084. static int
  4085. i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  4086. {
  4087. drm_i915_private_t *dev_priv, *next_dev;
  4088. struct drm_i915_gem_object *obj_priv, *next_obj;
  4089. int cnt = 0;
  4090. int would_deadlock = 1;
  4091. /* "fast-path" to count number of available objects */
  4092. if (nr_to_scan == 0) {
  4093. spin_lock(&shrink_list_lock);
  4094. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4095. struct drm_device *dev = dev_priv->dev;
  4096. if (mutex_trylock(&dev->struct_mutex)) {
  4097. list_for_each_entry(obj_priv,
  4098. &dev_priv->mm.inactive_list,
  4099. list)
  4100. cnt++;
  4101. mutex_unlock(&dev->struct_mutex);
  4102. }
  4103. }
  4104. spin_unlock(&shrink_list_lock);
  4105. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4106. }
  4107. spin_lock(&shrink_list_lock);
  4108. rescan:
  4109. /* first scan for clean buffers */
  4110. list_for_each_entry_safe(dev_priv, next_dev,
  4111. &shrink_list, mm.shrink_list) {
  4112. struct drm_device *dev = dev_priv->dev;
  4113. if (! mutex_trylock(&dev->struct_mutex))
  4114. continue;
  4115. spin_unlock(&shrink_list_lock);
  4116. i915_gem_retire_requests(dev);
  4117. list_for_each_entry_safe(obj_priv, next_obj,
  4118. &dev_priv->mm.inactive_list,
  4119. list) {
  4120. if (i915_gem_object_is_purgeable(obj_priv)) {
  4121. i915_gem_object_unbind(&obj_priv->base);
  4122. if (--nr_to_scan <= 0)
  4123. break;
  4124. }
  4125. }
  4126. spin_lock(&shrink_list_lock);
  4127. mutex_unlock(&dev->struct_mutex);
  4128. would_deadlock = 0;
  4129. if (nr_to_scan <= 0)
  4130. break;
  4131. }
  4132. /* second pass, evict/count anything still on the inactive list */
  4133. list_for_each_entry_safe(dev_priv, next_dev,
  4134. &shrink_list, mm.shrink_list) {
  4135. struct drm_device *dev = dev_priv->dev;
  4136. if (! mutex_trylock(&dev->struct_mutex))
  4137. continue;
  4138. spin_unlock(&shrink_list_lock);
  4139. list_for_each_entry_safe(obj_priv, next_obj,
  4140. &dev_priv->mm.inactive_list,
  4141. list) {
  4142. if (nr_to_scan > 0) {
  4143. i915_gem_object_unbind(&obj_priv->base);
  4144. nr_to_scan--;
  4145. } else
  4146. cnt++;
  4147. }
  4148. spin_lock(&shrink_list_lock);
  4149. mutex_unlock(&dev->struct_mutex);
  4150. would_deadlock = 0;
  4151. }
  4152. if (nr_to_scan) {
  4153. int active = 0;
  4154. /*
  4155. * We are desperate for pages, so as a last resort, wait
  4156. * for the GPU to finish and discard whatever we can.
  4157. * This has a dramatic impact to reduce the number of
  4158. * OOM-killer events whilst running the GPU aggressively.
  4159. */
  4160. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4161. struct drm_device *dev = dev_priv->dev;
  4162. if (!mutex_trylock(&dev->struct_mutex))
  4163. continue;
  4164. spin_unlock(&shrink_list_lock);
  4165. if (i915_gpu_is_active(dev)) {
  4166. i915_gpu_idle(dev);
  4167. active++;
  4168. }
  4169. spin_lock(&shrink_list_lock);
  4170. mutex_unlock(&dev->struct_mutex);
  4171. }
  4172. if (active)
  4173. goto rescan;
  4174. }
  4175. spin_unlock(&shrink_list_lock);
  4176. if (would_deadlock)
  4177. return -1;
  4178. else if (cnt > 0)
  4179. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4180. else
  4181. return 0;
  4182. }
  4183. static struct shrinker shrinker = {
  4184. .shrink = i915_gem_shrink,
  4185. .seeks = DEFAULT_SEEKS,
  4186. };
  4187. __init void
  4188. i915_gem_shrinker_init(void)
  4189. {
  4190. register_shrinker(&shrinker);
  4191. }
  4192. __exit void
  4193. i915_gem_shrinker_exit(void)
  4194. {
  4195. unregister_shrinker(&shrinker);
  4196. }